POJ 3461 Oulipo (KMP,求模版串在文本串中可覆盖出现的次数,constructive)

来源:互联网 发布:图像分割算法分类 编辑:程序博客网 时间:2024/06/03 11:59

Oulipo
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 24213 Accepted: 9711

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

3BAPCBAPCAZAAZAZAZAVERDIAVERDXIVYERDIAN

Sample Output

130


 典型的kmp算法

推荐一个牛逼的kmp介绍博客:http://blog.csdn.net/v_july_v/article/details/7041827

题目地址:http://poj.org/problem?id=3461

下面是大家博客中用到的办法:注意kmp函数中当j==len2时候将j=nextpos[j],这是一种对next数组合理的应用

//Hello. I'm Peter.#include<cstdio>#include<iostream>#include<sstream>#include<cstring>#include<string>#include<cmath>#include<cstdlib>#include<algorithm>#include<functional>#include<cctype>#include<ctime>#include<stack>#include<queue>#include<vector>#include<set>#include<map>using namespace std;typedef long long ll;typedef long double ld;#define peter cout<<"i am peter"<<endl#define input freopen("data.txt","r",stdin)#define randin srand((unsigned int)time(NULL))#define INT (0x3f3f3f3f)*2#define LL (0x3f3f3f3f3f3f3f3f)*2#define gsize(a) (int)a.size()#define len(a) (int)strlen(a)#define slen(s) (int)s.length()#define pb(a) push_back(a)#define clr(a) memset(a,0,sizeof(a))#define clr_minus1(a) memset(a,-1,sizeof(a))#define clr_INT(a) memset(a,INT,sizeof(a))#define clr_true(a) memset(a,true,sizeof(a))#define clr_false(a) memset(a,false,sizeof(a))#define clr_queue(q) while(!q.empty()) q.pop()#define clr_stack(s) while(!s.empty()) s.pop()#define rep(i, a, b) for (int i = a; i < b; i++)#define dep(i, a, b) for (int i = a; i > b; i--)#define repin(i, a, b) for (int i = a; i <= b; i++)#define depin(i, a, b) for (int i = a; i >= b; i--)#define pi 3.1415926535898#define eps 1e-6#define MOD 10007#define MAXN 1001000#define N 101000#define Mchar s[MAXN+N],subs[MAXN+N];int nextpos[MAXN+N];int len1,len2;void build_nextpos(){    int i,j;    i=0;    nextpos[0]=j=-1;    while(i<len2)    {        if(j==-1 || subs[i]==subs[j])        {            nextpos[i+1]=j+1;            i++;            j=j+1;        }        else j=nextpos[j];    }}int ans;void kmp(){    int i,j;    i=0,j=0;    ans=0;    while(i<len1)    {        if(j==-1 || s[i]==subs[j])        {            i++;            j++;        }        else j=nextpos[j];        if(j==len2)        {            ans++;            j=nextpos[j];        }    }}int main(){    int T;    cin>>T;    while(T--)    {        scanf("%s %s",subs,s);        len1=len(s);        len2=len(subs);        build_nextpos();        kmp();        printf("%d\n",ans);    }}


但,标题说了,constructive,有一个更有趣的办法

其实在做道题之前就已经想到了这个问题,可覆盖时候,求模版串在文本串中出现的位置。

我们可不可以将文本串拼接在模版串后面,并且在拼接处多加一个模版串和文本串都不可能出现的字符,比如这道题,加一个'@'

 然后利用next数组的定义(next[i]表示0到i-1这个串前缀后缀最大值)..懂了?

没错,当next[i]==len(len是模版串的长度)时候,就是一个解。哈哈,这个方法太巧妙了

/Hello. I'm Peter.#include<cstdio>#include<iostream>#include<sstream>#include<cstring>#include<string>#include<cmath>#include<cstdlib>#include<algorithm>#include<functional>#include<cctype>#include<ctime>#include<stack>#include<queue>#include<vector>#include<set>#include<map>using namespace std;typedef long long ll;typedef long double ld;#define peter cout<<"i am peter"<<endl#define input freopen("data.txt","r",stdin)#define randin srand((unsigned int)time(NULL))#define INT (0x3f3f3f3f)*2#define LL (0x3f3f3f3f3f3f3f3f)*2#define gsize(a) (int)a.size()#define len(a) (int)strlen(a)#define slen(s) (int)s.length()#define pb(a) push_back(a)#define clr(a) memset(a,0,sizeof(a))#define clr_minus1(a) memset(a,-1,sizeof(a))#define clr_INT(a) memset(a,INT,sizeof(a))#define clr_true(a) memset(a,true,sizeof(a))#define clr_false(a) memset(a,false,sizeof(a))#define clr_queue(q) while(!q.empty()) q.pop()#define clr_stack(s) while(!s.empty()) s.pop()#define rep(i, a, b) for (int i = a; i < b; i++)#define dep(i, a, b) for (int i = a; i > b; i--)#define repin(i, a, b) for (int i = a; i <= b; i++)#define depin(i, a, b) for (int i = a; i >= b; i--)#define pi 3.1415926535898#define eps 1e-6#define MOD 10007#define MAXN 1001000#define N 101000#define Mchar subs[MAXN+N];int nextpos[MAXN+N];int len;void build_nextpos(){    int i,j;    i=0;    nextpos[0]=j=-1;    while(i<len)    {        if(j==-1 || subs[i]==subs[j])        {            nextpos[i+1]=j+1;            i++;            j=j+1;        }        else j=nextpos[j];    }}int main(){    int T,len1,ans;    cin>>T;    while(T--)    {        scanf("%s",subs);        len1=len(subs);        subs[len1++]='@';        scanf("%s",subs+len1);        len=len(subs);        build_nextpos();        ans=0;        repin(i,len1,len)        {            if(nextpos[i]==len1-1) ans++;        }        printf("%d\n",ans);    }}


0 0
原创粉丝点击