POJ 3461 Oulipo (KMP,求模版串在文本串中可覆盖出现的次数,constructive)
来源:互联网 发布:图像分割算法分类 编辑:程序博客网 时间:2024/06/03 11:59
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
3BAPCBAPCAZAAZAZAZAVERDIAVERDXIVYERDIAN
Sample Output
130
典型的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 Mchar 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 Mchar 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); }}
- POJ 3461 Oulipo (KMP,求模版串在文本串中可覆盖出现的次数,constructive)
- (串的模式匹配4.6.2)POJ 3461 Oulipo(KMP算法的应用——求一个单词在一行文本中的出现次数)
- [KMP求模式在主串出现次数]POJ 3461 Oulipo
- POJ 3461 Oulipo(KMP:统计一个串出现的次数)
- POJ 3461 Oulipo(KMP 一个串在另一个串出现的次数(可重叠|不可))
- (KMP 1.2)hdu 1686 Oulipo(计算模式串在文本串中出现的次数)
- hdoj 1686 Oulipo 【求一个串在另一串的出现次数】【KMP】
- POJ 3461 Oulipo【KMP,子串出现次数,可重叠】
- POJ 3461 Oulipo(KMP统计子串出现次数)
- 1686 hdu Oulipo(求模式串在文本串中出现的次数)
- POJ 3461 Oulipo(KMP求匹配次数)
- POJ 3461 Oulipo (KMP字符串匹配·统计p在s中出现次数)
- poj 3461:Oulipo(KMP模版)
- POJ 3461 Oulipo【KMP】【模板题】(找出第一个字符串在第二个字符串中出现次数)
- KMP求模式串在原串中出现的次数
- KMP算法求模式串在原串中出现的次数
- POJ-2406 Power Strings(KMP求重复子串出现的最大次数)
- poj 3461 Oulipo(KMP)
- QDialog 模态对话框与事件循环
- 07-在mac os 系统下装windows双系统教程
- 改变listview中item选中时文字的颜色
- C++内存分配方式
- Android 实现蘑菇街购物车动画效果
- POJ 3461 Oulipo (KMP,求模版串在文本串中可覆盖出现的次数,constructive)
- ListView知识点总结
- hdoj 1204 I NEED A OFFER!
- hdoj Bone Collector dp问题
- SQL性能优化
- 利用虚拟索引(Virtual Index)优化数据库的案例分析
- 【Android】mac编译android-kernel-hammerhead
- 绞睬砍叛远哪辖直骋碳湃核
- 链表的反转