8.11 C

来源:互联网 发布:史上最恶搞游戏之网络 编辑:程序博客网 时间:2024/06/07 21:05

                                    C - A + B for you again


Generally speaking, there are a lot of problems aboutstrings processing. Now you encounter another such problem. If you get twostrings, such as “asdf” and “sdfg”, the result of the addition between them is“asdfg”, for “sdf” is the tail substring of “asdf” and the head substring ofthe “sdfg” . However, the result comes as “asdfghjk”, when you have to add“asdf” and “ghjk” and guarantee the shortest string first, then the minimumlexicographic second, the same rules for other additions.


Input

For each case, there are two strings (the chars selectedjust form ‘a’ to ‘z’) for you, and each length of theirs won’t exceed 10^5 andwon’t be empty.


Output

Print the ultimate string by the book.


Sample Input

asdf sdfg

asdf ghjk


Sample Output

asdfg

asdfghjk

 

题意:输入以空格隔开的两个字符串,判断第一个字符串后缀和第二个字符串前缀相等的情况,然后合并,合并后将重复的位置只输出一次,然后将两个字符串交换位置,重复上述比较,输出合并后长度最小的字符串,如果两次比较长度相等,将输入的字符串按照字典序判断输出合并后的字符串。




#include<stdio.h>#include<string.h>int next[600000];char t1[600000],t2[600000];void next_(char s1[]){memset(next,0,sizeof(next));//puts(s1);int i,j,n;n=strlen(s1);i=1;j=0;//next[0]=-1;for(i=1;i<n;){if(j==0&&s1[i]!=s1[j]){i++;}else if(j>0&&s1[i]!=s1[j]){j=next[j-1];}else{next[i]=j+1;i++;j++;}}/*for(i=0;i<n;i++)printf("%d  ",next[i]);printf("\n");*/}int kmp(char s1[],char s2[]){//puts(s1);printf(".%s\n",s2);int m,n,i,j;next_(s2);m=strlen(s1);n=strlen(s2);i=j=0;while(i<m){if(j==0&&s1[i]!=s2[j]){i++;}else if(j>0&&s1[i]!=s2[j]){j=next[j-1];}else{i++;j++;}}//if(i == m)return j;//return 0;}int main(){int i,j,k,m,n;while(scanf("%s%s",t1,t2)!=EOF){m=kmp(t1,t2);n=kmp(t2,t1);//printf("m===%d    n===%d\n",m,n);if(m==n){if(strcmp(t1,t2)<0)printf("%s%s\n",t1,t2+m);elseprintf("%s%s\n",t2,t1+m);}else if(m>n){printf("%s%s\n",t1,t2+m);}else{printf("%s%s\n",t2,t1+n);}}return 0;}









 

原创粉丝点击