hdoj 1686 Oulipo【求一个字符串在另一个字符串中出现次数】

Oulipo

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7141    Accepted Submission(s): 2835

Problem 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

#include<stdio.h>
#include<string.h>
#define MAX 1000100
char p[10010],str[MAX];
int f[10010];
void getfail()
{
	int i,j,len;
	len=strlen(p);
	f[0]=f[1]=0;
	for(i=1;i<len;i++)
	{
		j=f[i];
		while(j &&p [i]!=p[j])
		j=f[j];
		f[i+1]=p[i]==p[j]?j+1:0;
	}
}
int main()
{
	int n,m,j,i,t;
	int l1,l2;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%s%s",p,str);
		getfail();
		l1=strlen(p);
		l2=strlen(str);
		int s=0,j=0;
		for(i=0;i<l2;i++)
		{
			while(j&&str[i]!=p[j])
            j=f[j];
            if(str[i]==p[j])
            j++;
            if(j>=l1)
            {
                s++;
                j=f[j];//注意此处
            }
		}
		printf("%d\n",s);
	}
	return 0;
}

  借着此题,写下求失配函数的模板:

/*
*  f[]为失配函数数组
*
*/
getfail()
{
	int i,j;
    int len = strlen(p);
    f[0]=f[1]=1;
    for(i = 1; i < len; i++)
    {
    	j = f[i];
    	while(j && p[i] != p[j])
    	j = f[j];
    	f[i+1]= p[i] ==p[j]?j+1:0;
    }
}

  

时间: 2024-12-18 18:22:01

hdoj 1686 Oulipo【求一个字符串在另一个字符串中出现次数】的相关文章

HDU 1686 Oulipo 求大串中最多可匹配多少个小串(kmp)

http://acm.hdu.edu.cn/showproblem.php?pid=1686 Oulipo Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6098    Accepted Submission(s): 2448 Problem Description The French author Georges Perec (19

hdoj 1686 Oulipo【kmp】

Oulipo Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 80   Accepted Submission(s) : 51 Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without

hdoj 1686 Oulipo(KMP算法)

Oulipo http://acm.hdu.edu.cn/showproblem.php?pid=1686 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5903    Accepted Submission(s): 2370 Problem Description The French author Georges Perec (1

Hdoj 1686 Oulipo

/* Problem : Status : By wf, */ #include "algorithm" #include "iostream" #include "cstring" #include "cstdio" #include "string" #include "stack" #include "cmath" #include "queue&qu

【HDOJ】1686 Oulipo

kmp算法. 1 #include <cstdio> 2 #include <cstring> 3 4 char src[10005], des[1000005]; 5 int next[10005], total; 6 7 void kmp(char des[], char src[]){ 8 int ld = strlen(des); 9 int ls = strlen(src); 10 int i, j; 11 12 total = i = j = 0; 13 while (

字典序,求给定字符串的下一个

POJ 1146 ID Codes 给定字符串有26个小写字母组成,求出给定字符串的下一个. 算法简述: 对于字符数组chararray,从字符串末尾向前找到第一个chararray[index]<chararray[index-1],然后找到index之后的字符中最后一个大于chararray[index-1],记为position,小于chararray[index]的字符.然后交换chararray[index-1]和chararray[position].对index及其以后的字符从小到

hdu 1686 Oulipo

Oulipo http://acm.hdu.edu.cn/showproblem.php?pid=1686 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the l

hdoj 1686 kmp

题目: Sample Input 3 BAPC BAPC AZA AZAZAZA VERDI AVERDXIVYERDIAN Sample Output 1 3 0 代码: #include<iostream>#include<cstdio>#include<cstring>using namespace std;const int maxn1=1000010;const int maxn2=10010;int n,m;char a[maxn1];char b[maxn

HDU - 1686 Oulipo KMP匹配运用

HDU - 1686 Oulipo Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status Description The French author Georges Perec (1936?1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Ouli