Algorithm
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
int const N = 1e3 + 1;
char s[N], t[N];
int n, m, q;
vector<int> all;
int main() {
scanf("%d %d %d", &n, &m, &q);
scanf("%s\n%s", s, t);
int slen = strlen(s);
int tlen = strlen(t);
for(int i = 0; i + tlen - 1 < slen; ++i) {
bool ok = true;
for(int j = 0, k = i; j < tlen; ++j, ++k)
if(s[k] != t[j]) {
ok = false;
break;
}
if(ok)
all.push_back(i);
}
for(int i = 0, a, b; i < q; ++i) {
int res = 0;
scanf("%d %d", &a, &b);
--a, --b;
for(int j = 0; j < all.size(); ++j)
if(all[j] >= a && all[j] + tlen - 1 <= b)
++res;
printf("%d\n", res);
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
1
0
1
Demonstration
Codeforces Solution-B. Segment Occurrences-Solution in C, C++, Java, Python,Segment Occurrences,Codeforces Solution