Description
The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter ‘e’. He was a member of the Oulipo group. A quote from the book:
Tout avait Pair normal, mais tout s’affirmait faux. Tout avait Fair normal, d’abord, puis surgissait l’inhumain, l’affolant. Il aurait voulu savoir où s’articulait l’association qui l’unissait au roman : stir son tapis, assaillant à tout instant son imagination, l’intuition d’un tabou, la vision d’un mal obscur, d’un quoi vacant, d’un non-dit : la vision, l’avision d’un oubli commandant tout, où s’abolissait la raison : tout avait l’air normal mais…
Perec would probably have scored high (or rather, low) in the following contest. People are asked to write a perhaps even meaningful text on some subject with as few occurrences of a given “word” as possible. Our task is to provide the jury with a program that counts these occurrences, in order to obtain a ranking of the competitors. These competitors often write very long texts with nonsense meaning; a sequence of 500,000 consecutive 'T’s is not unusual. And they never use spaces.
So we want to quickly find out how often a word, i.e., a given string, occurs in a text. More formally: given the alphabet {‘A’, ‘B’, ‘C’, …, ‘Z’} and two finite strings over that alphabet, a word W and a text T, count the number of occurrences of W in T. All the consecutive characters of W must exactly match consecutive characters of T. Occurrences may overlap.
Input
The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:
One line with the word W, a string over {‘A’, ‘B’, ‘C’, …, ‘Z’}, with 1 ≤ |W| ≤ 10,000 (here |W| denotes the length of the string W).
One line with the text T, a string over {‘A’, ‘B’, ‘C’, …, ‘Z’}, with |W| ≤ |T| ≤ 1,000,000.
Output
For every test case in the input file, the output should contain a single number, on a single line: the number of occurrences of the word W in the text T.
Sample Input
3
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN
Sample Output
1
3
0
题意分析
就是给了一个word W 和一个text T,问W在T中出现的次数,或者理解为:给一个子串再给一个主串,问子串在主串中出现了多少次,在计算出现次数时可以是部分重叠。
样例中给了三个例子,第一个子串和主串都是“BAPC”,结果就是1
第二个例子,子串是“AZA”,主串是“AZAZAZA”,一共出现了3次
第三个例子,子串是“VERDI”,主串是“AVERDXIVYERDIAN”,出现了0次
这个题意相对好理解。
解题思路
这个和KMP本意差不多,KMP就是在主串中查找子串的算法,只是KMP强调的是查找子串过程中,如果失配时,不必回溯到前面的位置,而是保持主串中指针i不动,而子串的指针只需移动到next[j]位置就可以继续比较匹配。
这个题也是一样的,先把子串的next[]数组计算好,然后在主串中查找子串,失配时也是根据next[j]确定下一次对比的位置。当然,如果查找的过程中,已经匹配的字符数达到子串的长度,那就是找到了一个子串,这时下一次的查找时指针j也应该从next[j]这个位置开始(主串的指针i仍是保持不变)。
代码分析
数据结构:定义两个字符数组a[10010]和 b[1000010]分别用来存放子串和主串。题中说到子串的长度:1 ≤ |W| ≤ 10,000,主串长度: |W| ≤ |T| ≤ 1,000,000.
另外,定义一个整型的next数组 next[10010]。
还有一些指针、控制变量,如 i、j是指向主串、子串的指针;ans存放最后结果,lna、lnb存放串a和串b的长度,等等
代码如下:
char a[10010],b[1000010];
int next[10010];
int i,j,lna,lnb,x,y,T,ans;
计算next[]数组的代码和以前一样,因为要找共同前后缀,j和i分别指向当前子串左右两端,匹配过程中,i指针不断向右移动,依次计算每一个next[i],计算时j从串的左边开始一个个匹配,匹配成功,i++、j++继续匹配下一个,匹配不成功j=next[j]检查有没有更小的前后缀
for (i=2;i<=lnb;i++)
{
while (j>0&&a[i]!=a[j+1]) j=next[j];
if (a[i]==a[j+1]) j++;
next[i]=j;
}
next[]数组做好以后,就要利用next[]数组来在主串b中查找子串a,这时,指针i指向主串a,指针j指向子串b,
for (i=1;i<=n;i++)
{
while (j>0&&(j==lna||b[i]!=a[j+1])) j=next[j];
if (b[i]==a[j+1]) j++;
if (j==lna) ans++;
}
每次比较主串和子串对应的字符,如果匹配成功,j++、i++继续比较下一个字符。当然匹配成功后还要看看是不是已经比较到子串的末尾了(j==lna),如果是,那就是找到一个子串,ans++。
如果比较不一样,匹配失败了,那么下一次重新查找子串从什么位置开始呢?按照kmp算法,保持主串指针i不变,j指针根据数组next[]指向next[j]开始下一次查找就可以。
另外,如果刚找到了一个子串,接下来还要找下一个子串,同样也是让j=next[j]然后重新开始下一次查找就可以。所以当(j==lna)或者(b[i]!=a[j+1])时,都要j=next[j]
完整代码:
#include<cstdio>
#include<cstring>
char a[10010],b[1000010]; //a是子串,b是主串
int next[10010]; //next[]数组
int main()
{
int i,j,lna,lnb,T,ans;
scanf("%d",&T); //T代表要处理几组数据
while (T--)
{
ans=0; //最终结果ans初始化为0
scanf("%s%s",a+1,b+1); //读入子串a和主串b,下标从1开始
lna=strlen(a+1); //取子串a长度lna
lnb=strlen(b+1); //取主串b长度lnb
//---------计算生成next[]数组---------------
for (i=2,j=0;i<=lna;i++)
{
while (j>0&&a[i]!=a[j+1]) j=next[j];
if (a[i]==a[j+1]) j++;
next[i]=j;
}
//---------在主串b中查找子串a---------------
for (i=1,j=0;i<=lnb;i++)
{
while (j>0&&(j==lna || b[i]!=a[j+1])) j=next[j]; //j>0代表在已有部分匹配的前提下,已找到或失配时,下一次j的位置j=next[j]
if (b[i]==a[j+1]) j++; //当前字符匹配,j++,i在下一次for循环时++
if (j==lna) ans++; //如果子串全部匹配,找到一个子串,ans++
}
printf("%d\n",ans);
}
}
814

被折叠的 条评论
为什么被折叠?



