CodeForces 25E Test KMP
来源:互联网 发布:excel文档修复软件 编辑:程序博客网 时间:2024/06/05 01:54
题目链接:点击打开链接
题意:
给定3个字符串,进行拼接
重复的一段可以覆盖,问拼接后最小的长度(若一个串s1是s2的子串,则s1可以认为嵌入了s2内,不需要进行拼接
思路:
kmp搞一下。
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <math.h>#include <set>using namespace std;#define N 300005char T[N];//从0开始存 int f[N];//记录P的自我匹配 void getFail(int len, char *P) { int i = 0, j = -1; f[0] = -1; while(i != len) { if(j == -1 || P[i] == P[j]) f[++i] = ++j; else j = f[j]; } } int KMP(int *f2, char *S1, char *S2, int lens1, int lens2){ //f2是S2的失配数组 getFail(lens2, S2); int pos = 0, len = lens1, j = 0, i = 0;int gg = 0; while(i <= len) { while(j!=-1 && S1[i] != S2[j]) j = f2[j]; i++, j++;gg = max(j, gg); if(i == len) {pos = max(pos, j); } } if(gg == lens2)return gg; return pos; //这样得到的是S1的尾部和S2的前缀的 最大匹配位置(在S2中的位置) }/* */ char s[3][N/3];char tmp[N];int l[3];int hehe(int a,int b,int c){int pos = KMP(f, s[a], s[b], l[a], l[b]);for(int i = 0; i < l[a]; i++)tmp[i] = s[a][i];int top = l[a];if(pos!=l[b])for(; pos<l[b]; pos++)tmp[top++] = s[b][pos];tmp[top] = 0;pos = KMP(f, tmp, s[c], top, l[c]);return top+(l[c]-pos);}int main(){int i, j, u, v;while(~scanf("%s",s[0])){scanf("%s",s[1]);scanf("%s",s[2]);for(i=0;i<3;i++)l[i]=strlen(s[i]);int ans = hehe(0,1,2);ans = min(ans, hehe(0,2,1));ans = min(ans, hehe(1,0,2));ans = min(ans, hehe(1,2,0));ans = min(ans, hehe(2,0,1));ans = min(ans, hehe(2,1,0));cout<<ans<<endl;}return 0;}/*xufuzdlsjxmevrtessfbwlnzzclcqwevnnucxyvhngnxhcbdfwqwlwobhnmmgtfolfaeckbrnnglylydxtgtvrlmeeszoiuatzzzxufuzdlsjxmevrtbrnnglylydxtgtvrlmeeszoiuatzzzxsyvncqmfhautvxudqdhggzhrpxzeghsocjpicuixskfuzupytsgjsdiybybcmnmnbpndbxlxbzhbfnqvwcffvrdhtickyqhupmcehlsaaaaaaabcxababxcxabab*/
0 0
- CodeForces 25E Test KMP
- CodeForces 25E Test KMP
- CodeForces - 25E Test (KMP应用)
- CodeForces 25E Test
- CODEFORCES 25E Test
- CODEFORCES 25E Test
- CF-25E - Test(KMP)
- CodeForeces 25E (kmp)
- codeforces #25E
- codeforces #30E Tricky and Clever Password KMP+Manacher+二分
- codeforces #336 E. Marbles (字符串hash或者kmp匹配)
- codeforces 291 E. Tree-String Problem (dfs+kmp)
- Codeforces 25A IQ test
- 【CodeForces】[25A]IQ test
- Codeforces 30E Tricky and Clever Password Manacher + KMP + 前缀和
- kuangbin KMP E题
- CodeForces 825E Educational #25 E:拓扑排序+优先队列
- CodeForces 25A IQ test 唯一奇偶
- JFinal
- 求解Ax=b:可解性和解的结构
- uva 10368 - Euclid's Game(博弈)
- 素数筛选法(模板)
- 华为上机英文数字翻译
- CodeForces 25E Test KMP
- hdu-4656-So Easy!-递推式+矩阵优化
- 关于掉电保存数据的思考
- CAS SSO 工作机制(每一步请求详述)
- OpenCV学习笔记7 OpenCV核心模块与核心功能Core Module & Core Functionality(六)
- 编程语言类型划分
- C++ 内存管理(memory Management) part1
- Aurora中Problems running LaTex的解决方案 && Aurora2.x注册机
- JAVA正则表达式语法大全