Codeforces Beta Round #25 (Div. 2 Only)

来源:互联网 发布:速卖通翻译软件 编辑:程序博客网 时间:2024/06/05 16:28

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 s1s2 and s3 as substrings.

Examples
input
abbccd
output
4
input
abacabaabaabax
output
11


题意:

输入三个字符串,问你存在着三个字符串子串的字符串的最短长度。


POINT:

枚举A(3,3)种可能。

特殊情况若字符串包含,则处理一下。

#include <iostream>#include <stdio.h>#include <algorithm>#include <math.h>using namespace std;const int maxn = 1e5+55;#define LL long longint nxt[maxn];void prenxt(string s){int i=0,j;nxt[0]=j=-1;while(i<s.length()){while(j!=-1&&s[j]!=s[i]) j=nxt[j];nxt[++i]=++j;}}int kmp(string s1,string s2){prenxt(s2);int i=0,j=0;while(i<s1.length()){while(j!=-1&&s1[i]!=s2[j]) j=nxt[j];i++,j++;if(j==s2.length()) return (int)s2.length();}return j;}int ans=0x3f3f3f3f;void doit(string s1, string s2,string s3){int len=kmp(s1,s2);string s="";s+=s1;for(int i=len;i<s2.length();i++){s+=s2[i];}len=kmp(s,s3);ans=min(ans,(int)(s.length()-len+s3.length()));}int main(){string s1,s2,s3;cin>>s1>>s2>>s3;doit(s1,s2,s3);doit(s1,s3,s2);doit(s2,s1,s3);doit(s3,s1,s2);doit(s2,s3,s1);doit(s3,s2,s1);printf("%d\n",ans);}



原创粉丝点击