CF1200E Compress Words | 字符串hash

5
I want to order pizza
Iwantorderpizza
5
sample please ease in out
sampleaseinout
题意:如果前一个单词的后缀和第二个单词的前缀相同,那他们合并的时候省略前缀(看样例)。
题解:我们把前一个的后缀和后一个的前缀进行字符串hash,hash值一样的表示他们后缀和前缀相同可省略,用个pos标记第二个串从哪个位置开始要合并到前一个串上面,hash值相同时更新pos。
不知道为啥我用char数组写一直tle3,然后改成用string,结果wa1了????显示 “wrong answer Unexpected EOF in the participants output”于是在输入n的时候加了个多组输入终于过了。
代码:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e6 + ;
const ll P = ;
const ll mod = 1e9 + ;
string s,ans;
int main(){
int n;
while(~scanf("%d",&n)){
ans="\0";
while(n--) {
cin>>s;
int ls = s.size(),la = ans.size();
int len = min(la,ls),pos = ;
ll hs = ,ha = ,p = ;
for (int i = ; i < len; i++) {
ha = ((ans[la--i]*p%mod)+ha)%mod;
hs = ((hs*P%mod)+s[i])%mod;
if (ha == hs) pos = i+;
p=p*P%mod;
}
ans+=s.substr(pos);
}
cout<<ans<<"\n";
}
return ;
}
CF1200E Compress Words | 字符串hash的更多相关文章
- [知识点]字符串Hash
1.前言 字符串的几大主要算法都多少提及过,现在来讲讲一个称不上什么算法, 但是非常常用的东西——字符串Hash. 2.Hash的概念 Hash更详细的概念不多说了,它的作用在于能够对复杂的状态进行简 ...
- 【BZOJ-3555】企鹅QQ 字符串Hash
3555: [Ctsc2014]企鹅QQ Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1545 Solved: 593[Submit][Statu ...
- POJ 1200 字符串HASH
题目链接:http://poj.org/problem?id=1200 题意:给定一个字符串,字符串只有NC个不同的字符,问这个字符串所有长度为N的子串有多少个不相同. 思路:字符串HASH,因为只有 ...
- LA4671 K-neighbor substrings(FFT + 字符串Hash)
题目 Source http://acm.hust.edu.cn/vjudge/problem/19225 Description The Hamming distance between two s ...
- 各种字符串Hash函数比较(转)
常用的字符串Hash函数还有ELFHash,APHash等等,都是十分简单有效的方法.这些函数使用位运算使得每一个字符都对最后的函数值产生影响.另外还有以MD5和SHA1为代表的杂凑函数,这些函数几乎 ...
- 字符串hash + 二分答案 - 求最长公共子串 --- poj 2774
Long Long Message Problem's Link:http://poj.org/problem?id=2774 Mean: 求两个字符串的最长公共子串的长度. analyse: 前面在 ...
- 字符串hash - POJ 3461 Oulipo
Oulipo Problem's Link ---------------------------------------------------------------------------- M ...
- 长度有限制的字符串hash函数
长度有限制的字符串hash函数 DJBHash是一种非常流行的算法,俗称"Times33"算法.Times33的算法很简单,就是不断的乘33,原型如下 hash(i) = hash ...
- hdu 4622 Reincarnation 字符串hash 模板题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给定一个长度不超过2000的字符串,之后有不超过1e5次的区间查询,输出每次查询区间中不同 ...
随机推荐
- H3C IP及其相关协议
- hdu 1708 Fibonacci String
Fibonacci String Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- HDU 2066最短路径Dijkstra、
思路:枚举所有起点城市然后比较每个起点所去喜欢城市的最小距离 #include<cstdio> #include<cmath> #include<cstring> ...
- codeforces 609B
#include<bits/stdc++.h> using namespace std; ]; int main() { memset(num,,sizeof(num)); int n,m ...
- Python--day48--ORM框架SQLAlchemy操作表
ORM框架SQLAlchemy操作表: 表结构和数据库连接: #!/usr/bin/env python # -*- coding:utf-8 -*- from sqlalchemy.ext.decl ...
- 关于后台动态模板添加内容的总结 Builder使用
1.后台控制器中ArticleController中加载 formBuilder,listBuilder类: 2.实例化FormBuilder对象,调用类中的方法: 3.初始化时加载Builder/f ...
- P1098 方程解的个数
题目描述 给出一个正整数N,请你求出x+y+z=N这个方程的正整数解的组数(1<=x<=y<=z<1000).其中,1<=x<=y<=z<=N . 输入 ...
- 【9103】求n的累加和
Time Limit: 10 second Memory Limit: 2 MB 问题描述 用高精度方法,求s=1+2+3+...+n的精确值(n以一般整数输入) Input 文件输入仅一行,输入n ...
- UVA 1347"Tour"(经典DP)
传送门 参考资料: [1]:紫书 题意: 欧几里得距离???? 题解: AC代码: #include<bits/stdc++.h> using namespace std; ; int n ...
- 使用IDEA创建基于Maven SpringMvc项目
使用IDEA创建基于Maven SpringMvc项目 1.通过程序启动——create project,或者file--New-projec打开New project 2.自定义groupid等信息 ...