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

算法:Hash 或者 KMP

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

Hash:

#include <iostream>
#include <cstdio> using namespace std; typedef long long ll; const int maxn = 1e5+;
const int mod = ;
const int base = ; string ans, s; void solve() {
ll hl = , hr = , tmp = , index = ;
for(int i = ; i < (int)min(ans.size(), s.size()); i++) {
hl = (hl * base + ans[ans.size() - i - ]) % mod; //获取左边数组的后缀Hash值
hr = (s[i] * tmp + hr) % mod; //获取右边数组的前缀Hash值
tmp = (tmp * base) % mod;
if(hl == hr) {
index = i + ; //获取最大的字符匹配位数
}
}
for(int i = index; i < (int)s.size(); i++) {
ans += s[i];
}
} int main() {
int n;
scanf("%d", &n);
cin >> ans;
for(int i = ; i < n; i++) {
cin >> s;
solve();
}
cout << ans << endl;
return ;
}

E. Compress Words(Hash,KMP)的更多相关文章

  1. 【BZOJ1152】歌唱王国(生成函数,KMP)

    [BZOJ1152]歌唱王国(生成函数,KMP) 题面 BZOJ 洛谷 题解 根据\(YMD\)论文来的QwQ. 首先大家都知道普通型生成函数是\(\displaystyle \sum_{i=0}^{ ...

  2. 字符串学习总结(Hash & Manacher & KMP)

    前言 终于开始学习新的东西了,总结一下字符串的一些知识. NO.1 字符串哈希(Hash) 定义 即将一个字符串转化成一个整数,并保证字符串不同,得到的哈希值不同,这样就可以用来判断一个该字串是否重复 ...

  3. 搞定单模式匹配(简单,KMP)

    模式匹配是查找的一种,分为单模式匹配和多模式匹配.查找,就是在一个集合中查找一个或多个元素,查找一个元素就叫单模式匹配,查找多个元素就是多模式匹配,这里只探讨单模式匹配.虽然模式匹配看上去与数字的查找 ...

  4. 字符串匹配问题(暴力,kmp)

    对于字符串的匹配问题,现在自己能够掌握的就只有两种方法, 第一种就是我们常用的暴力匹配法,那什么是暴力匹配法呢? 假设我们现在有一个文本串和一个模式串,我们现在要找出模式串在文本串的哪个位置. 文本串 ...

  5. HDU 2087 剪花布条(字符串匹配,KMP)

    HDU 2087 剪花布条(字符串匹配,KMP) Description 一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出 ...

  6. HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP)

    HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP) Description The French author George ...

  7. 「学习笔记」字符串基础:Hash,KMP与Trie

    「学习笔记」字符串基础:Hash,KMP与Trie 点击查看目录 目录 「学习笔记」字符串基础:Hash,KMP与Trie Hash 算法 代码 KMP 算法 前置知识:\(\text{Border} ...

  8. c#一些处理解决方案(组件,库)

    1.关系数据库 postgresql,mysql,oracle,sqlserver 2.本地数据库 sqlite,berkeleydb,litedb 3.缓存数据库 redis,mongdb 4.数据 ...

  9. 【BZOJ3122】随机数生成器(BSGS,数论)

    [BZOJ3122]随机数生成器(BSGS,数论) 题面 BZOJ 洛谷 题解 考虑一下递推式 发现一定可以写成一个 \(X_{i+1}=(X_1+c)*a^i-c\)的形式 直接暴力解一下 \(X_ ...

随机推荐

  1. 18-Perl 错误处理

    1.Perl 错误处理程序运行过程中,总会碰到各式各样的错误,比如打开一个不存在的文件.程序运行过程中如果出现错误就会停止,我们就需要使用一些检测方法来避免错误,从而防止程序退出.Perl 提供了多中 ...

  2. 嵌套For循环性能优化

    请对以下的代码进行优化 for (int i = 0; i < 1000; i++) for (int j = 0; j < 100; j++) for (int k = 0; k < ...

  3. mysql中取出的时间日期多个.0

    字段名称为 create_time 字段类型为 datetime 存储的内容为 2019-03-26 09:42:05 但是 通过mybatis取出来放到实体类里,数值就变成了 2019-03-26 ...

  4. KVM命令记录

    创建qcow2镜像qemu-img create -f qcow2 /vm/kvm/img/vm41.img 500G 创建虚拟机virt-install --name=vm41 --disk pat ...

  5. FileStrem大文件分割复制

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  6. CTAP: Complementary Temporal Action Proposal Generation论文笔记

    主要观点:基于sliding window(SW)类的方法,如TURN,可以达到很高的AR,但定位不准:基于Group的方法,如TAG,AR有明显的上界,但定位准.所以结合两者的特长,加入Comple ...

  7. while 循环 及 and or not

    一  while 循环 语法: while 条件: 结果 如果条件为真,则直接执行结果,然后再次判断条件,直到条件是假,停止循环 结束循环: 1.改变条件 2.break 二 流程控制 break 和 ...

  8. Linux使用storcli工具查看服务器硬盘和raid组信息

    1.简介 MegaCli 是LSI公司官方提供的SCSI卡管理工具,由于LSI被收购变成了现在的Broadcom,所以现在想下载MegaCli, 需要去Broadcom官网查找Legacy产品支持,搜 ...

  9. Linux配置JDK环境

    wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-co ...

  10. zencart批量评论插件Easy Populate CSV add reviews使用教程

    此插件在Easy Populate CSV 1.2.5.7b产品批量插件基础上开发,有1.3x与1.5x两个版本. zencart批量评论插件Easy Populate CSV add reviews ...