1016B

题意:字符串a,字符串b,给你q个区间,让你求a的区间内字符串b出现了多少次

之前用的前缀数组做的,没想起来,发现这个其实也可以

 1 #include<cstdio>
 2 #include<cstring>
 3 int main()
 4 {
 5     int n,m,q;
 6     char s[1005],t[1005];
 7     int a[1005];
 8     while(~scanf("%d %d %d",&n,&m,&q))
 9     {
10         scanf("%s %s",s+1,t+1);
11         memset(a,0,sizeof(a));
12         int i,j;
13         for( i=1;i<=n;i++)
14         {
15             for(j=1;j<=m;j++)
16             {
17                 if(s[i+j-1]!=t[j])
18                     break;
19             }
20             if(j==m+1)
21                 a[i]=1;//如果出现了b字符串就把啊s[i]=t[0],a[i]=1

22 } 23 int ans,w,e; 24 for(i=1;i<=q;i++) 25 { 26 ans=0; 27 scanf("%d %d",&w,&e); 28 for(j=w;j<=e+1-m;j++)//找区间中b出现的次数,e+1-m在草稿纸上画一下就知道为什么了 29 { 30 if(a[j]==1) 31 ans++; 32 } 33 printf("%d ",ans); 34 } 35 } 36 return 0; 37 }
原文地址:https://www.cnblogs.com/LLLAIH/p/9703333.html