hdu1867---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): 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

2008杭电集训队选拔赛——热身赛

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: [email protected]
    > 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;
}
时间: 2024-10-15 17:57:54

hdu1867---A + B for you again的相关文章

HDU1867 A + B for you again【KMP】

A + B for you again Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9765 Accepted Submission(s): 2410 Problem Description Generally speaking, there are a lot of problems about strings processing.

HDU1867 A + B for you again KMP应用

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 "a

HDU1867:A + B for you again(KMP)

题意:找出一个最大的公共子串,这个子串是一个字符串的尾串(tail substring ),同时是另外那个字符串的头串(head substring),是满足A+B的长度strlen(A+B)达到最小值,这里面要注意的一个问题是,谁做模式串P是不一定的,所以要分别比较不同字符串作为模式的KMP值. 思路:让两个串分别做模式串.看谁KMP的结束的时候匹配的字符字符的个数最多就好. 当然也可以把两个串连接起来再求一次next[len1+len2]即可(求next相当于自我匹配),这时候要注意ans=

hdu1867(A + B for you again) 杭电java a题真坑

点击打开链接 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

HDU1867 A + B for you again(KMP)

A + B for you again Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7978    Accepted Submission(s): 2005 Problem Description Generally speaking, there are a lot of problems about strings process

hdu1867(kmp)

今天估计一直要刷kmp,kmp,kmp,kmp.... 这题目非常容易理解,就是A+B问题,不同的是,要找到A串后缀与B串前缀的最长串. 比如 ABC+BC -> ABC  , ABC+BCD =ABCD   ,ABCD+ BC=ABCDBC 用的就是kmp啦,输入两个串 str1 str2 ,以str1模式串,str2为文本匹配,以str2为模式串,str1为文本串,分别匹配出最长的长度.再比较. /***********************************************