E. Compress Words
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Amugae has a sentence consisting of 

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 1≤n≤105), the number of the words in Amugae's sentence.

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

 

 算法:Hash 或者 KMP

题解:首先分析题目,题目要求我们将每个连续单词相同的前后缀合并成一个,在我们学过的知识里面,字符串前后缀相同的匹配肯定第一想到的就是KMP,对,这题可以用KMP直接做,但是还有一种使代码更加简洁,思路更加简单的方法,那就是用Hash。只要每次查询的时候直接暴力求前后缀相同的最大字符数就行。

 

Hash:

#include <iostream>
#include <cstdio>

using namespace std;

typedef long long ll;

const int maxn = 1e5+7;
const int mod = 999999998;
const int base = 2333333;

string ans, s;

void solve() {
    ll hl = 0, hr = 0, tmp = 1, index = 0;
    for(int i = 0; i < (int)min(ans.size(), s.size()); i++) {
        hl = (hl * base + ans[ans.size() - i - 1]) % mod;   //获取左边数组的后缀Hash值
        hr = (s[i] * tmp + hr) % mod;       //获取右边数组的前缀Hash值
        tmp = (tmp * base) % mod;
        if(hl == hr) {
            index = i + 1;      //获取最大的字符匹配位数
        }
    }
    for(int i = index; i < (int)s.size(); i++) {
        ans += s[i];
    }
}  

int main() {
    int n;
    scanf("%d", &n);
    cin >> ans;
    for(int i = 1; i < n; i++) {
        cin >> s;
        solve();
    }
    cout << ans << endl;
    return 0;
}

 

 

相关文章:

  • 2021-10-01
  • 2021-10-30
  • 2022-12-23
  • 2022-02-20
  • 2021-12-29
  • 2021-12-18
  • 2021-08-19
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-14
  • 2022-12-23
  • 2022-12-23
  • 2021-06-12
  • 2021-12-18
相关资源
相似解决方案