Oulipo

poj3461:http://poj.org/problem?id=3461

题意:求一个串在另一个串中出现的次数。

题解:直接套用KMP即可,在统计的时候做一下修改。找到之后不是直接返回,而是移动i-(j-next[j])位。

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #define N 1000005
 5 using namespace std;
 6 int next[N];
 7 char s1[N],s2[N];
 8 int len1,len2,ans;
 9 void getnext(int plen){
10     int i = 0,j = -1;
11     next[0] = -1;
12     while( i<plen ){
13         if(j==-1||s1[i]==s1[j]){
14             ++i;++j;
15           if(s1[i]!=s1[j] )
16             next[i] = j;
17           else
18             next[i] = next[j];
19         }
20         else
21             j = next[j];
22     }
23 }
24 void KMP(){
25     getnext(len1);
26     int i = 0,j = 0;
27     while (i<len2&&j<len1){
28         if( j == -1 || s2[i] == s1[j] ){
29             ++i;
30             ++j;
31         }
32         else {
33             j = next[j];
34
35         }
36          if( j==len1 ){//找到一个匹配串之后,不是在原来串中往后移动一位,而是移动i-(j-next[j]);
37             ans++;
38             j=next[j];
39          }
40     }
41 }
42 int main(){
43     int k;
44     scanf("%d",&k);
45     while(k--){
46         scanf("%s%s",s1,s2);
47         len1=strlen(s1);
48         len2=strlen(s2);
49         ans=0;
50         KMP();
51         printf("%d\n",ans);
52     }
53 }

Oulipo,布布扣,bubuko.com

时间: 2025-01-16 06:54:45

Oulipo的相关文章

HDOJ-1686 Oulipo ---KMP

Oulipo Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5588    Accepted Submission(s): 2235 Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, wi

B - Oulipo

The French author Georges Perec (1936?C1982) 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, p

POJ 3461 Oulipo

E - Oulipo Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3461 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member

POJ 3461 Oulipo KMP算法题解

本题就是给出很多对字符串,然后问一个字符串在另外一个字符串出现的次数. 就是所谓的Strstr函数啦. Leetcode有这道几乎一模一样的题目. 使用KMP算法加速,算法高手必会的算法了. 另外看见讨论说什么使用KMP还超时,最大可能是没有真正理解next table的含义,写了错误的代码,故此虽然自己运行结果正确,但是却没有真正发挥next table的作用,使得算法退化为暴力法了,所以运行正确,但超时. KMP参考: http://blog.csdn.net/kenden23/articl

【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 (

POJ3461 Oulipo

Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36441   Accepted: 14728 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 quot

hdu 1686 Oulipo kmp算法

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目: 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

Match:Oulipo(POJ 3461)

 Oulipo 题目大意:给你一个字符串,要你找到字符串包含指定子串的个数 只要你知道了KMP,这一题简直不要太简单,注意STL的string是会超时的,还是乖乖用char吧 1 #include <iostream> 2 #include <algorithm> 3 #include <functional> 4 #include <string.h> 5 6 using namespace std; 7 8 typedef int Position; 9

KMP算法之Oulipo

Oulipo Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8137    Accepted Submission(s): 3280 Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, wit

KMP模式匹配算法: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,