E. Compress Words(Hash,KMP)
1 second
256 megabytes
standard input
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.
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.
In the only line output the compressed word after the merging process ends as described in the problem.
- 5
- I want to order pizza
- Iwantorderpizza
- 5
- sample please ease in out
- 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)的更多相关文章
- 【BZOJ1152】歌唱王国(生成函数,KMP)
[BZOJ1152]歌唱王国(生成函数,KMP) 题面 BZOJ 洛谷 题解 根据\(YMD\)论文来的QwQ. 首先大家都知道普通型生成函数是\(\displaystyle \sum_{i=0}^{ ...
- 字符串学习总结(Hash & Manacher & KMP)
前言 终于开始学习新的东西了,总结一下字符串的一些知识. NO.1 字符串哈希(Hash) 定义 即将一个字符串转化成一个整数,并保证字符串不同,得到的哈希值不同,这样就可以用来判断一个该字串是否重复 ...
- 搞定单模式匹配(简单,KMP)
模式匹配是查找的一种,分为单模式匹配和多模式匹配.查找,就是在一个集合中查找一个或多个元素,查找一个元素就叫单模式匹配,查找多个元素就是多模式匹配,这里只探讨单模式匹配.虽然模式匹配看上去与数字的查找 ...
- 字符串匹配问题(暴力,kmp)
对于字符串的匹配问题,现在自己能够掌握的就只有两种方法, 第一种就是我们常用的暴力匹配法,那什么是暴力匹配法呢? 假设我们现在有一个文本串和一个模式串,我们现在要找出模式串在文本串的哪个位置. 文本串 ...
- HDU 2087 剪花布条(字符串匹配,KMP)
HDU 2087 剪花布条(字符串匹配,KMP) Description 一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出 ...
- 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 ...
- 「学习笔记」字符串基础:Hash,KMP与Trie
「学习笔记」字符串基础:Hash,KMP与Trie 点击查看目录 目录 「学习笔记」字符串基础:Hash,KMP与Trie Hash 算法 代码 KMP 算法 前置知识:\(\text{Border} ...
- c#一些处理解决方案(组件,库)
1.关系数据库 postgresql,mysql,oracle,sqlserver 2.本地数据库 sqlite,berkeleydb,litedb 3.缓存数据库 redis,mongdb 4.数据 ...
- 【BZOJ3122】随机数生成器(BSGS,数论)
[BZOJ3122]随机数生成器(BSGS,数论) 题面 BZOJ 洛谷 题解 考虑一下递推式 发现一定可以写成一个 \(X_{i+1}=(X_1+c)*a^i-c\)的形式 直接暴力解一下 \(X_ ...
随机推荐
- 使用python操作zookeeper
kazoo 介绍 zookeeper的开发接口以前主要以java和c为主,随着python项目越来越多的使用zookeeper作为分布式集群实现,python的zookeeper接口也出现了很多,现在 ...
- isEmpty 和 isBlank 区别
isEmpty 和 isBlank 区别 org.apache.commons.lang.StringUtils 类提供了 String 的常用操作,最为常用的判空有如下两种 isEmpty(Stri ...
- 在react项目当中做导航守卫
距离上一篇文章,似乎已经过去好久了. 确实是最近相对忙了一点,本身是用vue重构之前一个传统的项目,就自己一个人写.而且,在稍微闲暇之余,想着同时用react也重构一遍,也算是对react的学习吧!毕 ...
- qt 静态编译配置项
configure -confirm-license -opensource -platform win32-msvc2013 -debug-and-release -static -prefix & ...
- H5移动端弹幕动画实现
思路 把单个内容编辑好,计算自身宽度,确定初始位置 移动的距离是屏幕宽度 js动态的添加css动画函数,将高度.动画移动时间.动画延迟时间都用随机数控制 代码: html骨架结构 (以三个为例,如果觉 ...
- 把json1赋值给json2,修改json2的属性,json1的属性也一起变化
let json1 = { a: 1}let json2 = json1json2.a = 5 console.log(json1.a) // 5 console.log(json2.a) // 5 ...
- HBuilderX 打包
新建 - 云打包 (密钥 密码看不到 - 回车) ( ) BlueStacks蓝叠 模拟器看效果
- Joomla 3.0.0 - 3.4.6 RCE漏洞分析记录
0x00 前言 今天早上看到了国内几家安全媒体发了Joomla RCE漏洞的预警,漏洞利用的EXP也在Github公开了.我大致看了一眼描述,觉得是个挺有意思的漏洞,因此有了这篇分析的文章,其实这个 ...
- windows2012获取明文密码
windows 2012获取明文密码 导hash的话用常规的方法就可以. 修改注册表 reg add HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Contr ...
- Java语言基础(10)
1 方法(三) 案例:Demo1 import java.util.Scanner; public class Demo1 { static int min(int num1,int num2){ i ...