HDOJ A + B for you again 1867【KMP】
2015-08-08 18:16
387 查看
A + B for you againTime Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5114 Accepted Submission(s): 1286 Problem Description 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. Input 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. Output Print the ultimate string by the book. Sample Input asdf sdfg asdf ghjk Sample Output asdfg asdfghjk Author Wang Ye Source 2008杭电集训队选拔赛——热身赛 Recommend lcy | We have carefully selected several similar problems for you: 1866 1277 1358 1753 1868 |
解题思路:
一开始是想利用next存取前缀后缀最大匹配的方式做,把后面的串放前面,这样就变成求前缀后缀最大匹配,结果写完发现自己想的样例过不了,各种纠结。后来去网上找解题报告发现题意理解不完全。。。。而且发现这样想是不对的,如果是cbc bcb 这样的最大前缀后缀后缀是4, 而题目并不是这个意思。所有,就要考虑加上KMP判断,找出最大的匹配位置就可以了。
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> #define maxn 100000+10 using namespace std; char P[maxn]; char S[maxn]; int pre[200010]; void getnext(char X[maxn],int xlen) { int i=0,j=-1; pre[0]=-1; while(i<xlen){ if(j==-1||X[i]==X[j]){ i++;j++; if(X[j]!=X[i]) pre[i]=j; else pre[i]=pre[j]; } else j=pre[j]; } } int kmpsearch(char *x,char *y) { int i=0,j=0; int xlen=strlen(x); int ylen=strlen(y); getnext(x,xlen); while(j<xlen&&i<ylen){ if(j==-1||x[j]==y[i]){ i++;j++; } else j=pre[j]; } if(i>=ylen)return j; return 0; } int main() { while(scanf("%s%s",P,S)!=EOF){ int l1=kmpsearch(P,S); int l2=kmpsearch(S,P); if(l1==l2){ if(strcmp(P,S)>0){ printf("%s%s\n",S,P+l1); } else printf("%s%s\n",P,S+l1); } else if(l1<l2){ printf("%s%s\n",P,S+l2); } else{ printf("%s%s\n",S,P+l1); } } return 0; }
相关文章推荐
- Problem F. Flat----(2015 summer training #5)
- Problem A. Automated Telephone Exchange----(2015 summer training #5)
- Teaching Mario to play with himself: AI, machine learning, and Super Mario Bros.
- ssh登陆提示"Host key verification failed."的解决方法
- hdoj 48119 Mosaic (二维线段树 单点更新)
- 使用GCC扩展attribute,在main函数外进行操作
- adb opendir failed,Permission denied
- HDU 1983 Kaitou Kid - The Phantom Thief (2) bfs and dfs
- [leetcode] Container With Most Water
- HDU 2473 Junk-Mail Filter(删点)
- 2015 Multi-University Training Contest 5 1009 模板
- 2015 HUAS Summer Trainning #4~B
- opencv waitkey使用注意点
- LeetCode(70)题解: climbing-stairs
- 使用Genymotion调试出现错误INSTALL_FAILED_CPU_ABI_INCOMPATI
- POJ 3007 Organize Your Train part II(枚举)
- uva 529 - Addition Chains
- Snail—1-9这9个数字划分成三个3位数,第一个分别是第二、三个的2倍,3倍
- Spartan6 slave SelectMap configuration fails owing to JTAG?
- JBoss 4.2 MBeans waiting for other MBeans (PersistenceUnit)