【字符串入门专题1】C
来源:互联网 发布:医学统计学简易软件 编辑:程序博客网 时间:2024/05/14 20:36
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 sdfgasdf ghjk
Sample Output
asdfgasdfghjk
题意:先按照前后缀长度,再按照字典序,输出两个字符串前缀和后缀最大公共长度。
思路:因为模式串和匹配串并不是固定给出,所以要交换两个字符串的身份调用kmp两次,计算它们的最大前缀和后缀长度k1,k2,当k1和k2相同时,就比较它们的字典序,否则,按匹配过后的顺序输出。
更正:这道题真的很有道理,如果按照我注释掉的代码提交虽然能AC,但是asdfadf 和df数据是不能过滴(代码又被小伙伴出的数据给搞掉了,让我想起了被zzq数据支配的噩梦)
#include<stdio.h>#include<string.h>#define N 110000char s1[N],s2[N];int next[N];void get_next(char *b){int j,k;next[0] = k = -1;j = 0;while(b[j]!='\0'){if(k==-1||b[j]==b[k])next[++j] = ++k;elsek = next[k];}return;}int kmp(char *a,char *b){int j,i;get_next(b);i = j = 0;while(a[i]!='\0'){while(j!=-1&&a[i]!=b[j])j = next[j];j++;i++;} return j;/*while(a[i]!='\0'&&b[j]!='\0'){while(j!=-1&&a[i]!=b[j])j = next[j];j++;i++;}printf("i=%d j=%d\n",i,j);if(i == strlen(a))return j;return 0;*/}int main(){int k1,k2;while(scanf("%s",s1)!=EOF){scanf("%s",s2);k1 = kmp(s1,s2);k2 = kmp(s2,s1);printf("k1=%d k2=%d\n",k1,k2);if(k1 == k2){if(strcmp(s1,s2)>0){printf("%s",s2);printf("%s\n",s1+k1);}else{printf("%s",s1);printf("%s\n",s2+k1);}}else if(k1 > k2){printf("%s",s1);printf("%s\n",s2+k1);}else{printf("%s",s2);printf("%s\n",s1+k2);}}return 0;}
阅读全文
0 0
- 【字符串入门专题1】C
- 【字符串入门专题1】A
- 【字符串入门专题1】 I
- 【字符串入门专题1】 E
- 【字符串入门专题1】J
- 【字符串入门专题1】B
- 【字符串入门专题1】F
- 【字符串入门专题1】D
- 【最小生成树入门专题1】C
- 【二分图匹配入门专题1】C
- 【字符串入门专题1】hdu3613 【一个悲伤的exkmp】
- 【搜索入门专题练习1】hdu1241+hdu1312 C+D【DFS】
- C指针专题1
- string专题(c++)【1】
- 【字符串入门专题1】【hdu4763】【未ac题】【扩展kmp exkmp】
- C语言入门----字符串
- reactjs入门专题(1)
- 【搜索入门专题1】I
- android 决TextView中MaxLines与ellipsize=end冲突问题
- 打印二维码(jdk1.5可用)
- 大数据实验——环境配置2
- IDEA 报 -Dmaven.multiModuleProjectDirectory system property is not set 解决办法
- ERP对企业有哪些作用?
- 【字符串入门专题1】C
- java学习笔记之线程1
- MySQL执行外部sql脚本文件的命令
- 递推求组合数
- Executor, ExecutorService 和 Executors 间的不同
- 线程池的用法及配置
- C标准库之二:ctype.h
- java--几种单例式
- python实例(斐波那契数列)