Oulipo
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 40 Accepted Submission(s) : 26
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.
#include<stdio.h>
#include<string.h>
#define M 1010000
char a[M],b[M];
int p[M];
int sum,len1,len2;
//P()是字串與字串的比較
void P()//求得字串與母串比較不相等時,字串返回的位置
{
int i,j;
i=0;j=-1;//規定字串第一個字符爲-1,第二個字符爲0。
p[i]=j;
while(i<len1)
{
if(j==-1||a[i]==a[j])
{
j++;i++;
p[i]=j;
}
else j=p[j];
}
}
void kmp()
{
P();
int i=0;
int j=0;
while(i<len2)
{
if(j==-1||a[j]==b[i])
{
i++;j++;
if(j==len1)
sum++;
}
else j=p[j];//每一次比較不相等時字串回到的位置
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s%s",a,b);
len1=strlen(a);
len2=strlen(b);
sum=0;
kmp();
printf("%d\n",sum);
}
return 0;
}