CF 8 A
来源:互联网 发布:excel跨工作簿引用数据 编辑:程序博客网 时间:2024/06/06 01:20
对KMP的熟练使用
View Code
我的代码 kmp匹配过去,需要熟练掌握KMP,数据范围大一点也没关系#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn = 100010;int n,mb,mc;char a[maxn],b[maxn],c[maxn];int p1[maxn],p2[maxn];void getp(int p[],int m,char b[]){ p[1]=0; int i,j=0; for(i=2;i<=m;i++){ while(j>0&&b[j+1]!=b[i]) j=p[j]; if(b[j+1]==b[i]) j+=1; p[i]=j; }}int kmp(int i,int p[],int m,char b[]){ int j=0; for(;i<=n;i++){ while(j>0&&b[j+1]!=a[i]) j=p[j]; if(b[j+1]==a[i]) j+=1; if(j==m){ return i; } } return -1;}int main(){ scanf("%s",a+1); n=strlen(a+1); int flag=0; scanf("%s",b+1);mb=strlen(b+1);getp(p1,mb,b); scanf("%s",c+1);mc=strlen(c+1);getp(p2,mc,c); int t=kmp(1,p1,mb,b); if(t!=-1 && t<n && kmp(t+1,p2,mc,c)!=-1) flag|=1; reverse(a+1,a+1+n); t=kmp(1,p1,mb,b); if(t!=-1 && t<n && kmp(t+1,p2,mc,c)!=-1) flag|=2; const char* ans[]={"fantasy","forward","backward","both"}; puts(ans[flag]); return 0;}网上的代码 用strstr,数据范围较大的情况可能效率就差了#include <cstdio>#include <cstring>#include <algorithm>using namespace std;char s[100500],a[200],b[200];int main(){ int n=strlen(gets(s)); int x=strlen(gets(a)); int y=strlen(gets(b)); int flag=0; if(strstr(s,a) && strstr(strstr(s,a)+x,b)) flag|=1; reverse(s,s+n); if(strstr(s,a) && strstr(strstr(s,a)+x,b)) flag|=2; const char* ans[]={"fantasy","forward","backward","both"}; puts(ans[flag]);}
- CF 8 A
- CF 8A Train and Peter
- cf 8a Train and Peter
- cf 8VC Venture Cup 2017 A
- CF 2A Winner
- CF 128A Statues
- <cf>A. Exams
- CF 109A
- CF 111A
- CF 4A Watermelon
- CF 237A (Cash)
- CF 6 A - Triangle
- CF:96A.Football
- CF:208A. Dubstep
- CF:41A. Translation
- CF:59A. Word
- CF:43A. Football
- CF:78A. Haiku
- CF 85D 五颗线段树
- poj 2785 哈希!!!
- 树链剖分初步 spoj 375 hdu 3966 codeforces 191C
- codeforces 123 div2
- STL序列式容器和关联式容器
- CF 8 A
- Never underestimate the heart of a champion
- 今天在CSDN开博了,记录技术学习心得
- Codeforces Round #124 (Div. 2) (Div.1)
- 程序员的第一篇博客
- 有更新 浙大月赛 ZOJ Monthly, June 2012 on June 24
- codeforces 127 div2
- codeforces 127 201C 双向DP
- poj 2831 次小生成树