我是靠谱客的博主 飞快哈密瓜,最近开发中收集的这篇文章主要介绍HDU 1867 A+B for you again(简单kmp)A + B for you again,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
A + B for you again
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8938 Accepted Submission(s): 2166
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:
1711
1866
1277
1865
1869
#include<iostream>
#include<queue>
#include<stdio.h>
#include<string>
#include<cmath>
using namespace std;
typedef long long LL;
#define maxn (100001*2)
int Next[maxn];
/*
题意很简单,
几个点就是如何判断字典序,
还有就是截取的问题,,,
这题该打::;我竟然忽略了最大前缀后缀可以覆盖其中一个串的情况,
处理方法是在中间添加一个分割符,,就可以了
*/
void getNext(string t)
{
int k=-1,sz=t.size();
Next[0]=-1;
for(int i=1;i<sz;i++)
{
while(k>-1&&t[k+1]!=t[i])
k=Next[k];
if(t[k+1]==t[i])
k++;
Next[i]=k;//i-k
}
}
int main()
{
ios::sync_with_stdio(false);
string s1,s2;
while(cin>>s1>>s2)
{
string tmp1=s2+"#"+s1,tmp2=s1+"#"+s2;
int len=tmp1.size();
getNext(tmp1);
int re1=Next[len-1]+1;
getNext(tmp2);
int re2=Next[len-1]+1;
tmp1=s1+s2.substr(re1,s2.size()-re1);
tmp2=s2+s1.substr(re2,s1.size()-re2);
if(re1>re2) cout<<tmp1;
else if(re2>re1) cout<<tmp2;
else
{
if(tmp1<tmp2) cout<<tmp1;
else cout<<tmp2;
}
cout<<endl;
}
return 0;
}
最后
以上就是飞快哈密瓜为你收集整理的HDU 1867 A+B for you again(简单kmp)A + B for you again的全部内容,希望文章能够帮你解决HDU 1867 A+B for you again(简单kmp)A + B for you again所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复