fork download
  1. /*
  2.   Copyright 2012 Marek "p2004a" Rusinowski
  3.   Naive pattern matching algorithm
  4. */
  5. #include <cstdio>
  6. #include <cstdlib>
  7. #include <cstring>
  8.  
  9. #define MAXN 1000000
  10.  
  11. int tab[MAXN], n, m;
  12. char str[MAXN], pat[MAXN];
  13.  
  14. void search(const char *str, const char *pat, int str_len, int pat_len, bool (*callback)(int idx, void *data), void *data = NULL) {
  15. for (int j, i = 0; i <= str_len - pat_len; ++i) {
  16. for (j = 0; j < pat_len; ++j) {
  17. if (str[i + j] != pat[j]) {
  18. break;
  19. }
  20. }
  21. if (j == pat_len && !callback(i, data)) {
  22. break;
  23. }
  24. }
  25. }
  26.  
  27. bool func(int a, void *) {
  28. printf("%d\n", a);
  29. return true;
  30. }
  31.  
  32. int main() {
  33. scanf("%d%d%s%s", &n, &m, str, pat);
  34. search(str, pat, n, m, func);
  35. return 0;
  36. }
  37.  
Success #stdin #stdout 0.02s 8544KB
stdin
20 4
asdbmamarmamamaasqqq  
mama
stdout
4
9
11