A + B for you again
Time Limit: 5000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1336Accepted Submission(s): 288
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
Sample Output
asdfgasdfghjk
/*开始没理解题意阿弥陀佛施主我太想呕吐了原来可以把前面的接到后面去郁闷没考虑到这点原来这就是最短的意识然后还得按最小字典
序输出只要写2个kmp即可让模式串在主串中搜索搜索到最后返回一个j就是相同的数量我们要把2个串分别当一次模式串找到j大的就是重复多的
下面我写的有点复杂了下面的函数都是重复的本来可以合成一个函数的*/
#include<stdio.h>
#include<string.h>
char a[100005];
char b[100005];
int next1[100005];
int next2[100005];
int d1,d2;
void get_next1()
{
int i=1,j=0;
next1[1]=0;
while(i<d2)
{
if(j==0||b[i]==b[j]) {i++;j++;next1[i]=j;}
else j=next1[j];
}
}
void get_next2()
{
int i=1,j=0;
next2[1]=0;
while(i<d1)
{
if(j==0||a[i]==a[j]) {i++;j++;next2[i]=j;}
else j=next2[j];
}
}
int KMP1()
{
int i=1,j=1;
while(i<=d1)
{
if(j==0||a[i]==b[j]) {i++;j++;}
else j=next1[j];
}
return j-1;
}
int KMP2()
{
int i=1,j=1;
while(i<=d2)
{
if(j==0||b[i]==a[j]) {i++;j++;}
else j=next2[j];
}
return j-1;
}
int main()
{
int cnt,flag,ans1,ans2;
while(scanf("%s",a+1)!=EOF)
{
cnt=0;flag=0;
scanf("%s",b+1);
d1=strlen(a+1);
d2=strlen(b+1);
get_next1();
ans1=KMP1();//a做主串
get_next2();
ans2=KMP2();
// printf("ans1=%d,ans2=%d\n",ans1,ans2);
if(ans1>ans2)
{
printf("%s%s\n",a+1,b+1+ans1);
}
else if(ans1<ans2)
{
printf("%s%s\n",b+1,a+1+ans2);
}
else
{
if(strcmp(a+1,b+1)>0)
printf("%s%s\n",b+1,a+1+ans1);
else printf("%s%s\n",a+1,b+1+ans1);
}
}
return 0;
}
分享到:
相关推荐
现在,给你两个正的小数A和B,你的任务是代表大明计算出A+B的值。 Input 本题目包含多组测试数据,请处理到文件结束。 每一组测试数据在一行里面包含两个长度不大于400的正小数A和B。 Output 请在一行里面...
本人准备2020年保研机试时刷的题目(虽然最后机试取消了,...来自某中流985,在HDU和vjudge平台上大概刷了400道。本文件地图(excel表格)包含了绝大部分我刷过的题目,笔记中具有思路、代码、总结和心得。 大佬勿入!
acm hdu as easy as a+b
2、new做两件事,一是分配内存,二是调用类的构造函数 3、new建立的是一个对象,而malloc分配的是一块内存 4、new/delete是保留字,不需要头文
300+ AC 代码 。 大数 , 线段树 , 字符串 , dp.....
ACM题库,一些题目和答案,以及解题报告,传上来共享
acm入门之枚举搜索,学校第一次acm培训,包括枚举及其优化,dfs和bfs
杭电OnlineJudge 200-2099的解题报告
hdu 1695 GCD(欧拉函数+容斥原理).docx
从简单入门到偏中等的几个题,线段树很灵活,主要懂了lazy操作,其他的自己yy吧。
杭电ACMhdu1163
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
hdu1001解题报告
HDU1059的代码
于是就问了一个大神,他的一个提示给了我灵感:虽然A、T、C、G的个数范围是[0,40],但是numa+numc+numg+numt的范围也是[0,40],于是就可以推出numa*numc*numg*numt的范围为[0,15000](自己写四个for循环可以算出最大...
hdu 1574 passed sorce
HDU的一题........HDU DP动态规
acm入门训练和日常训练 对于初学者以及acm爱好者有叫大帮助
hdu2101AC代码