POJ 3461 Oulipo(KMP 模板 一个串在另一个串出现的次数(可重叠|不可))
2017-12-14 22:20
531 查看
Oulipo
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
Sample Output
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;
}
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 44509 | Accepted: 17841 |
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; }
相关文章推荐
- POJ 3461 Oulipo【KMP】【模板题】(找出第一个字符串在第二个字符串中出现次数)
- POJ 3461 Oulipo【KMP,子串出现次数,可重叠】
- (串的模式匹配4.6.2)POJ 3461 Oulipo(KMP算法的应用——求一个单词在一行文本中的出现次数)
- POJ 3461 Oulipo (KMP字符串匹配·统计p在s中出现次数)
- POJ 3461 Oulipo(KMP统计子串出现次数)
- POJ 3461 Oulipo(KMP:统计一个串出现的次数)
- POJ 3461 Oulipo(KMP统计子串出现次数)
- [KMP求模式在主串出现次数]POJ 3461 Oulipo
- poj 3461 Oulipo(kmp统计子串出现次数)
- POJ 3461 KMP 模板题 字串出现次数
- POJ 3461 Oulipo (KMP,求模版串在文本串中可覆盖出现的次数,constructive)
- poj 3461 Oulipo(KMP模板题)
- POJ - 3461 Oulipo(KMP模板题)
- hdoj 1686 Oulipo 【求一个串在另一串的出现次数】【KMP】
- POJ 3461 Oulipo (KMP模板题)
- B - Oulipo poj 3461 (kmp 找子串的数,子串可以有重叠)
- HDU 2087 剪花布条【KMP,子串出现次数,不可重叠】
- POJ 3461 Oulipo(模式串在主串中出现的次数)
- poj 3461 Oulipo(KMP,喜闻乐见纯模板)
- POJ 3461 Oulipo(KMP模板题)