[字符串哈希/kmp]Compress Words Codeforces1200E

Amugae has a sentence consisting of nn words. He want to compress this sentence into one word. Amugae doesn't like repetitions, so when he merges two words into one word, he removes the longest prefix of the second word that coincides with a suffix of the first word. For example, he merges "sample" and "please" into "samplease".

Amugae will merge his sentence left to right (i.e. first merge the first two words, then merge the result with the third word and so on). Write a program that prints the compressed word after the merging process ends.

Input

The first line contains an integer nn (1≤n≤1051≤n≤105), the number of the words in Amugae's sentence.

The second line contains nn words separated by single space. Each words is non-empty and consists of uppercase and lowercase English letters and digits ('A', 'B', ..., 'Z', 'a', 'b', ..., 'z', '0', '1', ..., '9'). The total length of the words does not exceed 106106.

Output

In the only line output the compressed word after the merging process ends as described in the problem.

Examples

input

5
I want to order pizza

output

Iwantorderpizza

input

5
sample please ease in out

output

sampleaseinout

题意: 先给出n,表示接下来会给出n个单词,然后让你把n个单词合并为一个字符串。如果前面的单词后缀可以和后面的单词前缀匹配,则它们可以融合。例如sample和please,由于ple可以作为前后缀匹配,两单词合并后为samplease。

分析: 开两个字符串ans和t,ans表示最后的答案串,t表示当前需要处理的串。对于每一个t,遍历一遍能和ans匹配的长度,然后把不能匹配的子串加到ans中即可。注意遍历的时候长度应取min(len_t, len_ans),这样时间复杂度就在O(n)级别了。判断能否匹配时很容易想到字符串hash,维护ans串后缀hash和t串前缀hash即可,但是这道题卡了下单哈希,大部分常用组合都过不去(不信你们可以试试),如果非要单哈希选P = 2333,mod = 1e9+7也可以过。为了保险我选择双哈希。另外字符串匹配问题也可以想到kmp算法,以t为模式串,ans为文本串,当跑完一遍后j的值就表示t串中前j个字符可以与ans串中后j个字符匹配,即符合题意的前后缀。

具体代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#define P1 131
#define P2 233
using namespace std;

const long long mod1 = 1e9+9;
const long long mod2 = 1e9+7;
long long hash_ans1, hash_t1, hash_ans2, hash_t2, p1[1000005], p2[1000005];

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	//预处理p数组 
	p1[0] = p2[0] = 1;
	for(int i = 1; i <= 1000004; i++)
	{
		p1[i] = (p1[i-1]*P1)%mod1;
		p2[i] = (p2[i-1]*P2)%mod2;
	}
	int n;
	cin >> n;
	string ans, t;
	cin >> ans; 
	for(int i = 2; i <= n; i++)
	{
		cin >> t;
		int len = min(t.size(), ans.size()), len_ans = ans.size();
		int len_max = -1;//记录首尾最大能匹配长度 
		hash_t1 = hash_ans1 = hash_t2 = hash_ans2 = 0;
		for(int j = 0; j < len; j++)
		{
			//双哈希 
			hash_t1 = (hash_t1*P1+t[j])%mod1;
			hash_ans1 = (hash_ans1+ans[len_ans-1-j]*p1[j])%mod1;
			hash_t2 = (hash_t2*P2+t[j])%mod2;
			hash_ans2 = (hash_ans2+ans[len_ans-1-j]*p2[j])%mod2;
			if(hash_t1 == hash_ans1 && hash_t2 == hash_ans2)//记录下来最长的匹配长度 
				len_max = j;
		}
		ans += t.substr(len_max+1);//连到答案串上 
	}
	cout << ans;
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值