hdu1841 Find the Shortest Common Superstring-----KMP
来源:互联网 发布:js location.replace 编辑:程序博客网 时间:2024/05/17 00:11
Find the Shortest Common Superstring
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 971 Accepted Submission(s): 243
Problem Description
The shortest common superstring of 2 strings S1 and S2 is a string S with the minimum number of characters which contains both S1 and S2 as a sequence of consecutive characters. For instance, the shortest common superstring of “alba” and “bacau” is “albacau”.
Given two strings composed of lowercase English characters, find the length of their shortest common superstring.
Input
The first line of input contains an integer number T, representing the number of test cases to follow. Each test case consists of 2 lines. The first of these lines contains the string S1 and the second line contains the string S2. Both of these strings contain at least 1 and at most 1.000.000 characters.
Output
For each of the T test cases, in the order given in the input, print one line containing the length of the shortest common superstring.
Sample Input
2
alba
bacau
resita
mures
Sample Output
7
8
题意:求两字符串的最短和串。
首先我们设S1这个字符串比S2短,那么S2里包含S1是一种情况。
第二种情况:S1的尾覆盖S2的头
第三种情况:S2的尾覆盖S1的头
对于这两种情况,根据next函数的性质,我们只需要求出串C=S1S2的next[len1+len2],next[len1+len2]即为覆盖的长度,
同理求出C=S2S1的next[len1+len2]。笔记哦一下即可求得答案。
#include"stdio.h"#include"string.h"#include<iostream>#include<cstdlib>using namespace std;#define M 1000001int next[2*M];char c[2*M];char a[2][M];void get_next(char *s,int len){ int i,j; i=0;next[0]=-1;j=-1; while(i<len) { if(j==-1||s[i]==s[j]) { i++;j++; next[i]=j; } else { j=next[j]; } }}int index_KMP(char *s1,char *s2,int l1,int l2){ int i,j; i=0;j=0; while(i<l2&&j<l1) { if(j==-1||s2[i]==s1[j]) { i++;j++; } else { j=next[j]; } } if(j>=l1) return l2; else return 0;}int main(){ int n,len1,len2,temp,min,mat; while(scanf("%d",&n)!=EOF) { getchar(); while(n--) { gets(a[0]); gets(a[1]); len1=strlen(a[0]); len2=strlen(a[1]); if(len1>len2) { temp=len1; len1=len2; len2=temp; strcpy(c,a[0]); strcpy(a[0],a[1]); strcpy(a[1],c); } get_next(a[0],len1); min=index_KMP(a[0],a[1],len1,len2); if(min>0) { printf("%d\n",min); } else { strcpy(c,a[0]); strcat(c,a[1]); memset(next,0,sizeof(next)); get_next(c,len1+len2); min=len1+len2-next[len1+len2]; //cout<<next[len1+len2]<<"*"<<endl; strcpy(c,a[1]); strcat(c,a[0]); memset(next,0,sizeof(next)); get_next(c,len1+len2); mat=len1+len2-next[len1+len2]; // cout<<next[len1+len2]<<"*"<<endl; if(min<=mat) { printf("%d\n",min); } else { printf("%d\n",mat); } } } } return 0;}
- hdu1841 Find the Shortest Common Superstring-----KMP
- Find the Shortest Common Superstring(hdu1841,KMP)
- hdu1841 Find the Shortest Common Superstring -- (KMP 前缀,后缀处理)
- hdu1841 Find the Shortest Common Superstring 两遍KMP
- hdu1841 Find the Shortest Common Superstring
- HDU1841 Find the Shortest Common Superstring (KMP两字符串的首尾连接)
- HDU 1841 Find the Shortest Common Superstring KMP
- 题目1841 Find the Shortest Common Superstring(KMP)
- 文章标题 HDU 1841:Find the Shortest Common Superstring(KMP)
- HDOJ 1841 Find the Shortest Common Superstring(KMP)
- HDU 1841 Find the Shortest Common Superstring(KMP)
- hdu 1841 Find the Shortest Common Superstring
- HDU 1841: Find the Shortest Common Superstring
- HDU 1841Find the Shortest Common Superstring
- HDU 1841Find the Shortest Common Superstring
- HDU 1841 Find the Shortest Common Superstring(KMP灵活运用)
- hdoj1841Find the Shortest Common Superstring【扩展KMP模板】
- hdu1841Find the Shortest Common Superstring
- error C2679: 二进制“=”: 没有找到接受“LPCTSTR”类型的右操作数的运算符(或没有可接受的转换
- Tomcat + JSPWIKI: An user-extensible wiki-engine
- 刚参加工作的朋友可以看看
- [android开发必备]Android开发者社区汇总
- .net用户控件和页面的加载事件顺序
- hdu1841 Find the Shortest Common Superstring-----KMP
- CListCtrl列表中,改写几列的文字颜色
- Android中Timer类和TimerTask粗解
- 给明年依然年轻的我们
- 游戏角色设计
- poi笔记
- 各大互联网公司Android应用中TabHost的使用
- htm、html、shtml区别
- Android面试题