代码改变世界

hdu 1867 A + B for you again

2017-07-20 13:38  tlnshuju  阅读(178)  评论(0编辑  收藏  举报

不懂英文简直要是要死要死要死啊

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

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

英语不好是硬伤啊

#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;
}