CodeForces 25E Test KMP
2013-02-09 11:35
405 查看
E. Test
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Sometimes it is hard to prepare tests for programming problems. Now Bob is preparing tests to new problem about strings — input data to his problem is one string. Bob has 3 wrong
solutions to this problem. The first gives the wrong answer if the input data contains the substring s1,
the second enters an infinite loop if the input data contains the substring s2,
and the third requires too much memory if the input data contains the substring s3.
Bob wants these solutions to fail single test. What is the minimal length of test, which couldn't be passed by all three Bob's solutions?
Input
There are exactly 3 lines in the input data. The i-th
line contains string si.
All the strings are non-empty, consists of lowercase Latin letters, the length of each string doesn't exceed 105.
Output
Output one number — what is minimal length of the string, containing s1, s2 and s3 as
substrings.
Sample test(s)
input
output
input
output
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Sometimes it is hard to prepare tests for programming problems. Now Bob is preparing tests to new problem about strings — input data to his problem is one string. Bob has 3 wrong
solutions to this problem. The first gives the wrong answer if the input data contains the substring s1,
the second enters an infinite loop if the input data contains the substring s2,
and the third requires too much memory if the input data contains the substring s3.
Bob wants these solutions to fail single test. What is the minimal length of test, which couldn't be passed by all three Bob's solutions?
Input
There are exactly 3 lines in the input data. The i-th
line contains string si.
All the strings are non-empty, consists of lowercase Latin letters, the length of each string doesn't exceed 105.
Output
Output one number — what is minimal length of the string, containing s1, s2 and s3 as
substrings.
Sample test(s)
input
ab bc cd
output
4
input
abacaba abaaba x
output
11
#include<iostream> #include<cstdlib> #include<stdio.h> #include<string.h> using namespace std; #define N 200020 #define NN 100010 char str ; char s[3][NN]; int p ; int kmp(char *s1,char *s2)//返回s2中剩下的字符个数 { int n=strlen(s1); int m=strlen(s2); int j=-1; p[0]=-1; for(int k=1;k<m;k++) { while(j>=0&&s2[k]!=s2[j+1]) j=p[j]; if(s2[k]==s2[j+1]) j++; p[k]=j; } j=-1; for(int k=0;k<n;k++) { if(j==m-1) return 0; while(j>=0&&s1[k]!=s2[j+1]) j=p[j]; if(s1[k]==s2[j+1]) j++; } return (m-1-j); } int compair(char *s1,char *s2,char *s3) { int len=kmp(s1,s2); str[0]='\0'; strcat(str,s1); strcat(str,s2+strlen(s2)-len); len=kmp(str,s3); return strlen(str)+len; } int main() { while(scanf("%s%s%s",s[0],s[1],s[2])!=EOF) { int minn; int l; minn=compair(s[0],s[1],s[2]); // if(l<minn) minn=l; l=compair(s[0],s[2],s[1]); if(l<minn) minn=l; l=compair(s[1],s[2],s[0]); if(l<minn) minn=l; l=compair(s[1],s[0],s[2]); if(l<minn) minn=l; l=compair(s[2],s[0],s[1]); if(l<minn) minn=l; l=compair(s[2],s[1],s[0]); if(l<minn) minn=l; cout<<minn<<endl; } }
相关文章推荐
- CodeForces 25D Roads not only in Berland
- Codeforces 25 D.Roads not only in Berland(并查集)
- CodeForces 25D Roads not only in Berland
- CodeForces 25D Roads not only in Berland
- CodeForces 25BPhone numbers(简单的字符串模拟题目)
- Codeforces 25D-Roads not only in Berland 并查集
- CodeForces 25D - Roads not only in Berland(并查集题目)
- CodeForces 825G Educational Round #25 G :建树选根大法+O1大法+iostream解绑了还是慢
- Codeforces 25D-Roads not only in Berland(并查集)
- CodeForces 25D Roads not only in Berland
- codeforces 25 E Test KMP
- CodeForces 825F Educational Round #25 F:KMP最小循环节+DP
- CodeForces 25D Roads not only in Berland 并查集
- codeforces 417B - Crash
- CodeForces 159c String Manipulation 1.0
- Codeforces 424C Magic Formulas
- [知其然不知其所以然-25] How to setup systemtap
- CodeForces-719E Sasha and Array(线段树+矩阵快速幂)
- PAT A1021. Deepest Root (25)
- Codeforces - 715B. Complete The Graph - 构造最短路