【题目描述】
给定两个串A、B,可以得到从A的任意位开始的子串和B匹配的长度。
给定K个询问,对于每个询问给定一个X,求出匹配长度恰为X的位置有多少个(N,M,K <= 200000)。
【输入描述】
第一行输入三个数N、M、K,表示A的长度、B的长度和询问数;
第二行输入串A;
第三行输入串B;
接下来K行,每行输入一个数X。
【输出描述】
对于每次询问,输出一个数。
【样例输入】
6 2 2
aabcde
ab
0
2
【样例输出】
4
1
源代码: #include<cstdio> char S1[200001],S2[200001]; int n,m,k,Next[200001],Num[200001]; int main() { scanf("%d%d%d ",&n,&m,&k); gets(S1); //gets()读到回车结束。 gets(S2); Next[0]=Next[1]=0; for (int a=1;a<m;a++) { int T=Next[a]; while (T&&S2[T]!=S2[a]) T=Next[T]; Next[a+1]=T+(S2[T]==S2[a]); } int T(0); for (int a=0;a<n;a++) { while (T&&S1[a]!=S2[T]) T=Next[T]; if (S1[a]==S2[T]) T++; Num[T]++; } for (int a=m;a>0;a--) Num[Next[a]]+=Num[a]; for (int a=0;a<=m;a++) Num[a]-=Num[a+1]; for (int a=1;a<=k;a++) { int t; scanf("%d",&t); printf("%d ",Num[t]); } return 0; }