博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu A + B for you again
阅读量:4551 次
发布时间:2019-06-08

本文共 2572 字,大约阅读时间需要 8 分钟。

A + B for you again

Time Limit : 5000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 3   Accepted Submission(s) : 1
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
 
kmp的应用
代码
View Code
1 #include 
2 #include
3 #include
4 using namespace std; 5 const int Max=100010; 6 int next[Max]; 7 char str1[Max],str2[Max]; 8 void get_next(char *pat,int len) 9 {
10 int i=0,j=-1; 11 next[0]=-1; 12 while(i<=len) 13 {
14 if(j==-1||pat[i]==pat[j]) 15 {
16 i++,j++; 17 next[i]=j; 18 } 19 else 20 j=next[j]; 21 } 22 } 23 24 int kmp(char *s,char *pat) 25 {
26 int i = 0,j = 0,len1 = strlen(s),len2 = strlen(pat); 27 get_next(pat,len2); 28 while(i
=len1) 38 return j; 39 else 40 return 0; 41 } 42 int main() 43 {
44 while(scanf("%s%s",str1,str2)!=EOF) 45 {
46 int idx1,idx2; 47 idx2 = kmp(str1,str2); 48 idx1 = kmp(str2,str1); 49 // printf("idx2=%d idx1=%d\n",idx2,idx1); 50 if( idx1 == idx2) 51 {
52 if(strcmp(str1,str2) < 0) 53 {
54 printf("%s%s",str1,str2+idx2); 55 } 56 else 57 {
58 printf("%s%s",str2,str1+idx1); 59 } 60 } 61 else if( idx2 > idx1 ) 62 {
63 printf("%s%s",str1,str2+idx2); 64 } 65 else 66 {
67 printf("%s%s",str2,str1+idx1); 68 } 69 printf("\n"); 70 } 71 return 0; 72 }

转载于:https://www.cnblogs.com/one--world--one--dream/archive/2011/11/06/2238316.html

你可能感兴趣的文章
矩阵快速幂优化线性递推
查看>>
基础网络流学习笔记
查看>>
Linux文件夹文件创建、删除
查看>>
归并排序 稳定
查看>>
xml约束技术之dtd
查看>>
云存储的那些事(1)——数据冗余
查看>>
android状态机机制StateMachine
查看>>
滚动条自适应宽度的问题
查看>>
第二次作业——个人项目实战
查看>>
HighCharts图表控件在ASP.NET WebForm中的使用
查看>>
C#汉字转拼音
查看>>
Remote Service 和 Local App的交互
查看>>
mysql删除重复数据
查看>>
文件下载工具类
查看>>
Python 定义自己的常量类
查看>>
C++读取文本文件
查看>>
Python 字典排序
查看>>
sql中写标量函数生成大写拼音首字母
查看>>
ASP.NET Core 2.1 : 十五.图解路由(2.1 or earler)
查看>>
服务器返回状态码说明
查看>>