Codeforces Beta Round #25 (Div. 2 Only)E. Test
2015-08-24 20:02
344 查看
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
KMP
给你3个子串,求最短的原串。
例如
abc
ab
c
最短原串就是abc
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
KMP
给你3个子串,求最短的原串。
例如
abc
ab
c
最短原串就是abc
/* *********************************************** Author :pk29 Created Time :2015/8/23 18:41:02 File Name :4.cpp ************************************************ */ #include <iostream> #include <cstring> #include <cstdlib> #include <stdio.h> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <iomanip> #include <list> #include <deque> #include <stack> #define ull unsigned long long #define ll long long #define mod 90001 #define INF 0x3f3f3f3f #define maxn 100000+10 #define cle(a) memset(a,0,sizeof(a)) const ull inf = 1LL << 61; const double eps=1e-5; using namespace std; int f[maxn]; void getFail(string p,int *f){ int m=p.size(); f[0]=0; f[1]=0; for(int i=1;i<m;i++){ int j=f[i]; while(j&&p[i]!=p[j])j=f[j]; f[i+1]=p[i]==p[j]?j+1:0; } } int find(string t,string p,int *f){ int n=t.size(),m=p.size(); getFail(p,f); int j=0; for(int i=0;i<n;i++){ while(j&&p[j]!=t[i])j=f[j]; if(p[j]==t[i])j++; if(j==m)return 0;//一开始没考率到主串包含子串,wa成狗 } return m-j; } string s[3]; int arr[3]={0,1,2}; int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif //freopen("out.txt","w",stdout); while(cin>>s[0]>>s[1]>>s[2]){ if(s[0]>s[1])swap(s[0],s[1]); if(s[1]>s[2])swap(s[1],s[2]); if(s[0]>s[1])swap(s[0],s[1]); //next_permutation()使用前得先排序 int ans=INF; int num=1; do{ string t=""; t+=s[0]; int a=find(t,s[1],f); t+=s[1].substr(s[1].size()-a,a); int b=find(t,s[2],f); int x=t.size()+b; ans=min(ans,x); }while(next_permutation(s,s+3)); cout<<ans<<endl; } return 0; }
相关文章推荐
- 彻底明白Java的多线程-实现线程同步
- 腾讯微博分享的时候,无法进行授权。
- HDU I'm Telling the Truth (二分图最大匹配+字典序最大路径输出(好题))
- hdoj 1286 找新朋友 【欧拉函数】
- poj 1995 Raising Modulo Numbers 【快速幂】
- CentOS 6下HBase安装与使用
- Android PopupWindow的使用和分析
- Spiral Matrix I&&II
- java分类和几个术语
- C源码@数据结构与算法->BinomialQueue
- Linux学习
- iOS检测版本更新
- PHP位运算符
- TMS320F241在混合动力汽车电机控制设计应用
- 使用ImageMagick的convert命令,实现批量rgb转cmyk
- dubbo学习之Hello world
- Sqlite学习笔记(四)&&SQLite-WAL原理
- Nginx从入门到精通
- 虚拟机上为ubuntu 扩充硬盘容量
- Mac OSX 10.10搭建nginx+mysql+php-fpm环境