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
  1. 5
  2. I want to order pizza
output
  1. Iwantorderpizza
input
  1. 5
  2. sample please ease in out
output
  1. sampleaseinout

算法:Hash 或者 KMP

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

Hash:

  1. #include <iostream>
  2. #include <cstdio>
  3.  
  4. using namespace std;
  5.  
  6. typedef long long ll;
  7.  
  8. const int maxn = 1e5+;
  9. const int mod = ;
  10. const int base = ;
  11.  
  12. string ans, s;
  13.  
  14. void solve() {
  15. ll hl = , hr = , tmp = , index = ;
  16. for(int i = ; i < (int)min(ans.size(), s.size()); i++) {
  17. hl = (hl * base + ans[ans.size() - i - ]) % mod; //获取左边数组的后缀Hash值
  18. hr = (s[i] * tmp + hr) % mod; //获取右边数组的前缀Hash值
  19. tmp = (tmp * base) % mod;
  20. if(hl == hr) {
  21. index = i + ; //获取最大的字符匹配位数
  22. }
  23. }
  24. for(int i = index; i < (int)s.size(); i++) {
  25. ans += s[i];
  26. }
  27. }
  28.  
  29. int main() {
  30. int n;
  31. scanf("%d", &n);
  32. cin >> ans;
  33. for(int i = ; i < n; i++) {
  34. cin >> s;
  35. solve();
  36. }
  37. cout << ans << endl;
  38. return ;
  39. }

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. Python(七) —— mock接口开发

    mock接口开发 接口开发有很多框架,诸如 Django,flask,相比较而言,flask 是轻量级web开发框架,用来开发 mock 接口的不二之选.那你可能会问,什么叫 mock 接口呢?moc ...

  2. nop4.1学习ServiceCollectionExtensions(二)(ioc,ef,ef连接的实现)

    这个是获取程序路径,并初始化文件管理类 初始化插件管理 接下来就是注册服务和autoafc 在INopStartup配置sql连接,插件,mvc等 配置了sql连接 数据库的配置类 在AddAutoM ...

  3. Sublime Text 3配置浏览默认路径为localhost

    1.在 Sublime Text 3 中,安装 SideBarEnhancements 侧边栏增强插件.(注意:安装插件之前需要安装包管理工具,参考这里) 2.SideBarEnhancements ...

  4. 数值或者电话号码被EXCEL转成了科学计数法,用XSSFCell 如何读取

    public static Map<String, Integer> readXls() throws IOException { //用来获取每一个小号重复多次,被多少账号用了.来平均 ...

  5. 打包JavaFX11桌面应用程序

    打包JavaFX11桌面应用程序 这是JavaFX系列的第二弹,第一弹在这里 在第一弹中,我们使用的是OpenJDK8,但是OpenJDK8和Oracle Java JDK不一样,它没有内置JavaF ...

  6. Java并发编程——线程池

    本文的目录大纲: 一.Java中的ThreadPoolExecutor类 二.深入剖析线程池实现原理 三.使用示例 四.如何合理配置线程池的大小 一.Java中的ThreadPoolExecutor类 ...

  7. 【JavaScript】js中的构造函数,和构造函数的实例中的原型详解

    1. 构造函数作为一个对象,他也有他的原型属性,即.prototype:该属性指向的原型对象是Object. 2. 而构造函数产生的实例却没有.prototype属性,但是作为一个对象,该实例却仍然拥 ...

  8. Java ArrayList常用接口介绍及示例

    Java List 常用类型 类型 特征 ArrayList 随机访问元素快:中间插入与删除元素较慢:操作不是线程安全的 LinkedList 中间插入与删除操作代价较低,提供优化的顺序访问:随机访问 ...

  9. nginx 配置简单的静态页面

    nginx 文件服务配置,MIME和 default_type https://blog.csdn.net/qq_26711103/article/details/81116900 nginx 静态页 ...

  10. Java学习笔记【十、泛型】

    简介 Java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型安全检测机制,该机制允许程序员在编译时检测到非法的类型. 泛型的本质是参数化类型,也就是说所操作的数据 ...