HDUOJ---1867 A + B for you again
2013-12-17 12:32
344 查看
A + B for you again
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3432 Accepted Submission(s): 869[align=left]Problem Description[/align]
Generally speaking, there are a lot of problems about strings processing. Now you encounter another such problem. If you get two strings, 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 of the “sdfg” . However, the result comes as “asdfghjk”, when you have to add “asdf” and “ghjk” and guarantee the shortest string first, then the minimum lexicographic second, the same rules for other additions.
[align=left]Input[/align]
For each case, there are two strings (the chars selected just form ‘a’ to ‘z’) for you, and each length of theirs won’t exceed 10^5 and won’t be empty.
[align=left]Output[/align]
Print the ultimate string by the book.
[align=left]Sample Input[/align]
asdf sdfg
asdf ghjk
[align=left]Sample Output[/align]
asdfg
asdfghjk
[align=left]Author[/align]
Wang Ye
简述一下题目的意思:对于s和t两个串,将这两个串合并为一个串,但是要满足下面的 规则:
比如: 如果s串在前,t在后, 且s串后缀和t串的前缀有重合的部分,合并这部分....比如 sdsds sdsa --> sdsdsa
但是要满足下面的要求:
s和t 随意组合,可以s在前,t在后,亦可以t在前s在后,但是必须报保证s+t的串长度最小,若果两者组合的长度相等,则按照字典序的顺序组合输出....
比如 abc cdea -->abcdea
对此,我们可以用kmp来ac就可以了...
代码如下:
#include <stdio.h> #include <string.h> #define maxn 100000 int next[maxn+1]; char ps[maxn+1],pt[maxn+1]; void get_next(char const * t,int *next,int const lent) { int i=0,j=-1; memset(next,0,sizeof(next)); next[0]=-1; while(i<lent) { if(j==-1||t[i]==t[j]) { ++i; ++j; if(t[i]!=t[j]) next[i]=j; else next[i]=next[j]; } else j=next[j]; } } int ext_kmp(char const * ps, char const *pt,int const lens,int const lent) { int i=lens-lent-1,j=-1; get_next(pt,next,lent); if(i<-1)i=-1; while(i<lens) { if(j==-1||ps[i]==pt[j]) { ++i; ++j; } else j=next[j]; } return j; } int main() { int i,ansa,ansb; int lens,lent; while(scanf("%s%s",ps,pt)!=EOF) { lens=strlen(ps); lent=strlen(pt); ansa=ext_kmp(ps,pt,lens,lent); ansb=ext_kmp(pt,ps,lent,lens); if(ansa>ansb) { printf("%s",ps); for(i=ansa;i<lent;i++) printf("%c",pt[i]); } else if(ansa<ansb) { printf("%s",pt); for(i=ansb;i<lens;i++) printf("%c",ps[i]); } else { if(strcmp(ps,pt)<0) { printf("%s",ps); for(i=ansa;i<lent;i++) printf("%c",pt[i]); } else { printf("%s",pt); for(i=ansb;i<lens;i++) printf("%c",ps[i]); } } putchar(10); } return 0; }
View Code
[align=left]Recommend[/align]
相关文章推荐
- hdu 1867 A + B for you again (KMP)
- hdu1867—A + B for you again
- 【HDOJ】1867 A + B for you again
- HDU 1867 A + B for you again (KMP应用)
- HDU 1867 A+B for you again
- HDU 1867 A + B for you again(简单KMP)
- HDU 1867 A + B for you again 字符串拼接 kmp
- HDU 1867:A + B for you again
- HDU 1867 A + B for you again(KMP)
- HDU 1867 A + B for you again 数据结构+KMP简单应用
- HDU 1867 A + B for you again 字符匹配
- HDU 1867 A + B for you again (KMP)
- hdu 1867 A + B for you again
- 杭电1867 A + B for you again
- hdu 1867 A + B for you again(KMP)
- 1867 A + B for you again
- HDU 1867--A + B for you again【KMP】
- hdu 1867 A + B for you again
- KMP——HDU 1867 A + B for you again
- hdu - 1867 - A + B for you again