E. Compress Words(KMP)

E. Compress Words

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Amugae has a sentence consisting of n 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 n (1≤n≤105), the number of the words in Amugae’s sentence.

The second line contains n 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 106.

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

代码:


#include<iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <functional>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <algorithm>
#define ll int
#define mes(x,y) memset(x,y,sizeof(x))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
const ll INFF = 2000000;
const ll maxn = 2e6 + 10;
ll Next[maxn];
char a[maxn], b[maxn];
ll lena, lenb;
void GetNext() {
	Next[0] = -1;
	Next[1] = 0;
	ll i = 1, k = 0;
	while (i < lenb) {
		if (k < 0 || b[i] == b[k]) Next[++i] = ++k;
		else k = Next[k];
	}
}
ll KMP(ll flag) {
	GetNext();
	ll k = 0, i = flag;
	while (i < lena) {
		if (k < 0 || a[i] == b[k])++i, ++k;
		else k = Next[k];
		if (k == lenb)return k;
	}
	return k;
}
int main() {
	FAST_IO;
	ll n, m, k, i, j, vb;
	string s;
	while (cin>>n) {
		cin >> a;
		vb = 0;
		lena = strlen(a);
		for (i = 1; i < n; i++) {
			//mes(Next, 0);
			cin >> b;
			lenb = strlen(b);
			m = KMP(max(vb - lenb, 0));
			//	cout<<m<<endl;
			for (j = m; j < lenb; j++) {
				a[lena++] = b[j];
			}
			vb = lena;
		}
		cout << a << endl;
	}


}

#include<iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <functional>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <algorithm>
#define ll long long
#define PI acos(-1)
#define mes(x,y) memset(x,y,sizeof(x))
#define lp pair<ll, ll>
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
ll n, m, i, j, k, t, flag, x, y, z;
const ll INF = 5e6 + 30;
string s1, s2;
ll kmp[INF];
void KMP() {
	kmp[0] = -1;kmp[1] = 0;
	ll i = 1, k = 0;
	while (i < s2.length()) {
		if (k < 0 || s2[i] == s2[k])kmp[++i] = ++k;
		else k = kmp[k];
	}
}
ll FindFlag(ll i) {
	KMP(); ll k = 0;
	while (i < s1.length()) {
		if (k < 0 || s1[i] == s2[k])i++, k++;
		else k = kmp[k];
		if (k == s2.length())return k;
	}
	return k;
}
int main() {
	FAST_IO;
	while (cin >> n >> s1) {
		for (i = 1; i < n; i++) {
			cin >> s2;
			int x = s1.length()-s2.length();
			//这里必须转换,
			//原本写的是j = FindFlag((s1.length() - s2.length() > 0 ? s1.length() - s2.length() : 0));
			//就是不行,一直wa6,怀疑是max函数炸了
			j = FindFlag((x > 0 ? x : 0));
			if (j < s2.length())s1 += s2.substr(j);
		}
		cout << s1 << endl;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GUESSERR

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值