您的位置:首页 > 其它

POJ 3461 Oulipo(KMP 模板 一个串在另一个串出现的次数(可重叠|不可))

2017-12-14 22:20 531 查看
Oulipo

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 44509 Accepted: 17841
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

Source

BAPC 2006 Qualification

      KMP算法第一题,巧了的和我开始看的入门是一个意思,但是没做对,因为刚开始我就发现了的一个问题,判断出现次数的时候是不是可以重叠的呢?那些入门博客因为不涉及题只是讲解思路所以没有多说,但是在第一个题里就不可避免的遇到了,不过很好解决。现在看我自己写的那一个简陋代码,我自己在到达条件时多加上了个k=0,这里就是把k的值往前提了,以致于得到的结果是不能重叠的,而想要不能重叠只需要把这句话去掉就好了。其他的部分,包括两个函数,我都看了其他人的题解,对过程加以了优化,原来的while循环可以去掉了,换成了一个并查集的样子,应该是能够优化不少的。

    这里的next【】数组,求出来的是 -1.。。。。结果是存到(1,len)里的,不是(0,len-1)!!!k表示:配对了k个字符    k最大是lent!!   

具体在代码里面:

新代码:

#include<iostream>
#include<cstring>
using namespace std;
char sw[10010];
char st[1000010];
int next[10010];
void getNext()
{
next[0]=-1;
int lenw=strlen(sw);
int temp=-1;//
int j;
for(j=0; j<lenw; )
{
if(temp==-1 || sw[j]==sw[temp])//比较当前位j与next[j] 的字符是否相等 结束条件
{
j++;//下一位
temp++;//temp跟上
next[j]=temp;//求得下一位的值
}
else
temp=next[temp];//找next[next[]],并查集思想
}
}
int getKMP()
{
int lenw=strlen(sw);
int lent=strlen(st);
getNext();
/*cout<<"*********** next *****"<<endl;
cout<<" ";
for(int i=0; i<=lenw; i++)
cout<<i<<" ";
cout<<endl;
for(int i=0; i<=lenw; i++)
cout<<next[i]<<" ";
cout<<endl;*/
int ans=0;
int k=0;//k表示 配对了k个字符 k最大是lent!!
for(int i=0; i<lent; )
{
if(k==-1 || st[i]==sw[k])
{
i++;
k++;
if(k==lenw)
{
ans++;
//k=0; //加上这句话就可以 求不能重叠的
}
}
else
k=next[k];
}
return ans;
}
int main()
{
int T;
cin>>T;
while(T--)
{
cin>>sw;
cin>>st;
cout<<getKMP()<<endl;
}
return 0;
}

#include<iostream>
#include<cstring>
using namespace std;
char sw[10010];
char st[1000010];
int next[10010];
void getNext()
{
next[0]=-1;
int lenw=strlen(sw);
int temp=-1;//
int j=0;
while(j<lenw)
{
if(temp==-1 || sw[j]==sw[temp])//比较当前位j与next[j] 的字符是否相等    结束条件
{
j++;//下一位
temp++;//temp跟上
next[j]=temp;//求得下一位的值
}
else
temp=next[temp];//找next[next[]],并查集思想
}
}
int getKMP()
{
int lenw=strlen(sw);
int lent=strlen(st);
getNext();
int ans=0;
int i=0;
int k=0;
while(i<lent)
{
if(k==-1 || st[i]==sw[k])
{
i++;
k++;
if(k==lenw)
{
ans++;
//k=0;   //加上这句话就可以 求不能重叠的
}
}
else
k=next[k];
}
return ans;
}
int main()
{
int T;
cin>>T;
while(T--)
{
cin>>sw;
cin>>st;
cout<<getKMP()<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: