B - Oulipo HDU-1686

题目大意:

给一个t,接下来每个t两个串,求能匹配的次数。

解题思路:

修改模板kmp,不要j==len就结束while,而是在i==len才结束,由于这里是可重叠的匹配,所以 j==len时,j=next[j],ans++。

参考代码:

 1 #include <iostream>
 2 #include <vector>
 3 #include <map>
 4 #include <string>
 5 #include <queue>
 6 #include <stack>
 7 #include <set>
 8 #include <algorithm>
 9
10 #include <cstdio>
11 #include <cstring>
12 #include <cmath>
13 #include <cstdlib>
14 using namespace std;
15
16 const int INF=0x3f3f3f3f;
17 const int SIZE=10000;
18 typedef long long LL;
19
20 char a[1000005];
21 char b[10005];
22 int nextt[10005];
23
24 void nexxt()
25 {
26     memset(nextt,0,sizeof(nextt));
27     int j=0,k=-1;
28     nextt[0]=-1;
29     int len=strlen(b);
30     while(j<len)
31     {
32         if(k==-1||b[j]==b[k])
33         {
34             ++k;
35             ++j;
36             if(b[j]!=b[k])
37                 nextt[j]=k;
38             else
39                 nextt[j]=nextt[k];
40         }
41         else
42             k=nextt[k];
43     }
44 }
45
46 int kmp()
47 {
48     int i=0,j=0,ans=0;
49     int la=strlen(a),lb=strlen(b);
50     while(i<la)
51     {
52         //printf("i:%d j:%d\n",i,j);
53         if(j==-1||a[i]==b[j])
54         {
55             i++;
56             j++;
57         }
58         else
59             j=nextt[j];
60         if(j==lb)
61         {
62             j=nextt[j];
63             ans++;
64         }
65     }
66     return ans;
67
68 }
69
70 int main()
71 {
72     int t;
73     scanf("%d",&t);
74     getchar();
75     while(t--)
76     {
77          gets(b);
78          gets(a);
79          nexxt();
80          printf("%d\n",kmp());
81     }
82     return 0;
83 }

时间: 2024-10-21 22:51:43

B - Oulipo HDU-1686的相关文章

[kuangbin带你飞]专题十六 KMP &amp; 扩展KMP &amp; Manacher B - Oulipo HDU - 1686(kmp)

B - Oulipo HDU - 1686 题目链接:https://vjudge.net/contest/70325#problem/B 题目: 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 Pa

Oulipo HDU - 1686

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, pu

HDU 1686 Oulipo(KMP)

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

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

hdu 1686 Oulipo (kmp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目大意:寻找子链在母链中出现的次数. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 int next[10010],sum,lens,lenc; 6 char str[10010],ch[1000010]; 7 8 int get_nex

hdu 1686 KMP模板

1 // hdu 1686 KMP模板 2 3 // 没啥好说的,KMP裸题,这里是MP模板 4 5 #include <cstdio> 6 #include <iostream> 7 #include <cstring> 8 #include <algorithm> 9 10 using namespace std; 11 12 const int MAX_N = 1000008; 13 const int MAX_M = 10008; 14 char T

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

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

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

HDU 1686 Oulipo(kmp)

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