您的位置:首页 > 大数据 > 人工智能

hdu 1867 A + B for you again

2015-08-18 17:28 447 查看
不懂英文简直要是要死要死要死啊

首先是谁是谁的字串问题,这个取决于重构的字符串的长度最小

其次是当无论谁是谁的字串,重构字符串的长度都相等时,去字典序小的重构字符串

英语不好是硬伤啊

#include<iostream>
#include<string>
#include<algorithm>
#define maxn 100100
using namespace std;
string str;
int nextt[maxn];
void kmp()
{
int l=0,k=-1;
nextt[0]=-1;
while(l<str.size())
{
if(k==-1||str[l]==str[k]) nextt[++l]=++k;
else k=nextt[k];
}
}
int solve(string a,string b)//返回字串需要截取掉的位置
{
str=b;
kmp();
int i=0,j;
if(a.size()>b.size()) j=a.size()-b.size();
else j=0;
while(j<a.size())
{
if(i==-1||a[j]==b[i])
{
i++;j++;
}
else i=nextt[i];
}
return i;
}
int main()
{
cin.sync_with_stdio(false);
string a,b;
while(cin>>a>>b)
{
int x=solve(a,b);
int y=solve(b,a);
if(x==y)
{
if(a<b)
{
b.erase(0,x);
cout<<a<<b<<endl;
}
else
{
a.erase(0,y);
cout<<b<<a<<endl;
}
}
else if(x>y)
{
b.erase(0,x);
cout<<a<<b<<endl;
}
else
{
a.erase(0,y);
cout<<b<<a<<endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: