CF1200E Compress Words | 字符串hash
2024-09-06 08:45:34
Examples
input 1
5
I want to order pizza
output 1
Iwantorderpizza
input 2
5
sample please ease in out
output 2
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 ;
}
最新文章
- 通过arcgis在PostgreSQL中创建企业级地理数据库
- Django进阶2
- SGU 495. Kids and Prizes
- java.sql.SQLException: null, message from server: “Host ‘xxx’ is not allowed to connect
- 实验5 简单嵌入式WEB服务器实验 实验报告 20135303 20135326
- POJ 2429 GCD &; LCM Inverse (Pollard rho整数分解+dfs枚举)
- 元组的cmp()内建函数
- Customer reviews on Lexia3 V48 diagnostic tool in EOBD2.FR
- excel 导入功能
- 【原】Windows中使用Redis基本入门教程
- GCC优化选项-fomit-frame-pointer对于esp和ebp优化的作用
- MYSQL create database 和 create table 做了一些什么!
- js获取网页屏幕可见区域高度
- http请求的概念
- FFT算法的完整DSP实现(转)
- 分享一些免费的MD5解密网站
- Dojo初探之2:设置dojoConfig详解,dojoConfig参数详解+Dojo中预置自定义AMD模块的四种方式(基于dojo1.11.2)
- JSR-303 Bean Validation 介绍及 Spring MVC 服务端参数验证最佳实践
- 网络爬虫技术Jsoup——爬到一切你想要的(转)
- 理解WebKit和Chromium: Web应用和Web运行环境