hdu1867---A + B for you again

94 篇文章 0 订阅

A + B for you again

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4633    Accepted Submission(s): 1192


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
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   1866  1277  1865  1869  2057 
 

Statistic | Submit | Discuss | Note


这题我是用扩展kmp做的, 做两次就行


/*************************************************************************
    > File Name: hdu1867.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年02月02日 星期一 17时03分34秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

const int N = 200110;

char S[N], T[N];
int next[N];
int extend[N];
char A1[N], A2[N];

void EXTEND_KMP (char S[], char T[])
{
	int lens = strlen (S);
	int lent = strlen (T);
	next[0] = lent;
	int i, j, p, L;
	j = 0;
	while (j + 1 < lent && T[j] == T[j + 1]) // 先求出next[1]
	{
		++j;
	}
	next[1] = j;
	int a = 1;
	for (i = 2; i < lent; ++i)
	{
		p = next[a] + a - 1;
		L = next[i - a];
		if (i + L < p + 1)
		{
			next[i] = L;
		}
		else
		{
			j = max (0, p - i + 1);
			while (i + j < lent && T[i + j] == T[j])
			{
				++j;
			}
			next[i] = j;
			a = i;
		}
	}
	j = 0;
	while (j < lens && S[j] == T[j])
	{
		++j;
	}
	extend[0] = j;
	a = 0;
	for (i = 1; i < lens; ++i)
	{
		p = extend[a] + a - 1;
		L = next[i - a];
		if (L + i < p + 1)
		{
			extend[i] = L;
		}
		else
		{
			j = max(0, p - i + 1);
			while (i + j < lens && j < lent && S[i + j] == T[j])
			{
				++j;
			}
			extend[i] = j;
			a = i;
		}
	}
}

int main ()
{
	while (~scanf("%s%s", S, T))
	{
		EXTEND_KMP (S, T);
		int lens = strlen (S);
		int lent = strlen (T);
		int s = -1;
		strcpy (A1, S);
		for (int i = 0; i < lens; ++i)
		{
			if (extend[i] + i == lens)
			{
				s = i;
				break;
			}
		}
		if (s == -1)
		{
			strcat (A1, T);
		}
		else
		{
			int cnt = lens;
			for (int i = lens - s; i < lent; ++i)
			{
				A1[cnt++] = T[i];
			}
			A1[cnt] = '\0';
		}
		memset (next, 0, sizeof(next));
		memset (extend, 0, sizeof(extend));
		strcpy (A2, T);
		EXTEND_KMP (T, S);
		s = -1;
		for (int i = 0; i < lent; ++i)
		{
			if (extend[i] + i == lent)
			{
				s = i;
				break;
			}
		}
		if (s == -1)
		{
			strcat (A2, S);
		}
		else
		{
			int cnt = lent;
			for (int i = lent - s; i < lens; ++i)
			{
				A2[cnt++] = S[i];
			}
			A2[cnt] = '\0';
		}
		int len1 = strlen (A1);
		int len2 = strlen (A2);
		if (len1 < len2)
		{
			printf("%s\n", A1);
		}
		else if (len1 > len2)
		{
			printf("%s\n", A2);
		}
		else
		{
			if (strcmp (A1, A2) < 0)
			{
				printf("%s\n", A1);
			}
			else
			{
				printf("%s\n", A2);
			}
		}
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值