Time Limit: 1000MS | Memory Limit: 65536K | |
Total 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
3 BAPC BAPC AZA AZAZAZA VERDI AVERDXIVYERDIAN
Sample Output
1 3 0
典型的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 M
char 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 M
char 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);
}
}