cf#342-B - War of the Corporations
2016-02-15 00:33
232 查看
http://codeforces.com/contest/625/problem/B
题意:给出s子串
和t子串
求 s子串要替换多少个字符为#才能使得 在s里面完全找不到t
直接kmp找出所有的 不重复子串个数即可
裸的kmp啦。。
题意:给出s子串
和t子串
求 s子串要替换多少个字符为#才能使得 在s里面完全找不到t
直接kmp找出所有的 不重复子串个数即可
裸的kmp啦。。
#include <cstdio> #include <cmath> #include <cstring> #include <string> #include <algorithm> #include <queue> #include <map> #include <set> #include <vector> #include <iostream> using namespace std; #define lson l , m , rt << 1 #define rson m + 1 , r , rt << 1 | 1 const __int64 inf=2147483647; const double pi=acos(-1.0); double eps=0.000001; __int64 min(__int64 a,__int64 b) {return a<b?a:b;} __int64 max(__int64 a,__int64 b) { return a<b?b:a; } const int maxn = 100005; int nextval[maxn]; void get_next(char *t,int len) //失配函数 { int i,j; i=1; nextval[1]=0; j=0; while(i<len) { if (j==0||t[i]==t[j]) { j++; i++; if (t[i]!=t[j]) nextval[i]=j; else nextval[i]=nextval[j]; } else j=nextval[j]; } } int kmp(char* s,char* t,int len_s,int len_t) { int i=1; int j=1; while(i<=len_s&&j<=len_t) { if (j==0||s[i]==t[j]) { i++;j++; } else j=nextval[j]; } if (j>len_t) return i-len_t; else return 0; } char tm[maxn]; char nm[500]; int main( ) { scanf("%s",tm+1); scanf("%s",nm+1); int n=strlen(tm+1); int m=strlen(nm+1); get_next(nm,m); int cun=0; int st=0; while(1) { int ret= kmp(tm+st,nm,n,m); if (!ret) break; else { cun++; st+=ret+m-1; } } printf("%d\n",cun); return 0; }
相关文章推荐
- 读《大学之路》有感①
- 安卓json开发误区
- cf#342-A-Guest From the Past-贪心
- 【HDOJ】4251 The Famous ICPC Team Again
- define与typedef一点点
- 学习记录
- 【bzoj1014】[JSOI2008]火星人prefix splay+hash+二分
- 自定义测试执行器
- 自定义测试执行器
- Codeforces 626A Robot Sequence
- ubuntu lamp配置多域名服务器
- 数组做函数形参时
- ipvs负载均衡(四)ipvs三种工作方式之tun模式
- Codeforces 626B Cards(规律)
- 1050. String Subtraction (20)
- HDU-ACM-2041
- 欢迎使用CSDN-markdown编辑器
- mini售票系统之创建线程的三种方式
- android proxy settings with lantern
- 常用的三类IP地址