codeforces 631D. Messenger kmp
首先想到kmp, 和普通的不一样的是,中间部分严格相等, 头和尾的字符相等但是数量可以不相等。 所以应该把子串的头和尾先去掉,然后对剩下的部分进行kmp。
子串长度为1或2要特别讨论。
不要忘记一开始先把相邻的相同的部分合并掉。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 2e5+;
pair <ll, char> a[maxn], b[maxn];
#define next nextt
int n, m, next[maxn];
void init_kmp() {
int i = , j = -;
next[] = -;
while(i<m) {
if(j == - || b[i] == b[j]) {
++i, ++j;
next[i] = j;
} else {
j = next[j];
}
}
}
int main()
{
int x;
char ch;
cin>>n>>m;
for(int i = ; i<n; i++) {
scanf("%I64d-%c", &x, &ch);
a[i] = mk(x, ch);
}
for(int i = ; i<m; i++) {
scanf("%I64d-%c", &x, &ch);
b[i] = mk(x, ch);
}
int cnt = ;
for(int i = ; i<n-; i++) {
if(a[i].se == a[i+].se) {
a[cnt].fi += a[i+].fi;
continue;
}
a[++cnt] = a[i+];
}
n = cnt+;
cnt = ;
for(int i = ; i<m-; i++) {
if(b[i].se == b[i+].se) {
b[cnt].fi += b[i+].fi;
continue;
}
b[++cnt] = b[i+];
}
m = cnt+;
ll ans = ;
if(m == ) {
for(int i = ; i<n; i++) {
if(a[i].se == b[].se&&a[i].fi>=b[].fi) {
ans += (a[i].fi-b[].fi+);
}
}
cout<<ans<<endl;
return ;
}
if(m == ) {
for(int i = ; i<n-; i++) {
if(a[i].se == b[].se && a[i+].se == b[].se && a[i].fi>=b[].fi&&a[i+].fi>=b[].fi) {
ans ++;
}
}
cout<<ans<<endl;
return ;
}
pair <ll, char> s, e;
s = b[], e = b[m-];
cnt = ;
for(int i = ; i<m-; i++) {
b[cnt++] = b[i]; //去掉头尾
}
m -= ;
init_kmp();
int i = , j = ;
while(i<n-) {
if(j == - || a[i] == b[j]) {
i++, j++;
} else {
j = next[j];
}
if(j == m) {
if(s.se == a[i-j-].se && s.fi<=a[i-j-].fi
&& a[i].se == e.se && a[i].fi>=e.fi) {
ans++;
}
j = next[j];
}
}
cout<<ans<<endl;
return ;
}
codeforces 631D. Messenger kmp的更多相关文章
- Codeforces 631D Messenger【KMP】
题意: 给定由字符串块(字符及连续出现的个数)组成的字符串t,s,求t串中有多少个s. 分析: KMP 这题唯一需要思考的地方就是如何处理字符串块.第一想到是把他们都展开然后进行KMP,可是展开后实在 ...
- CodeForces 631D Messenger —— (kmp的应用)
这题是一个kmp的应用,思路是有,但是代码实现能力太弱,细节考虑不全,敲了很长时间才AC.. 题意:字符串用如下的方法表示,例如aaabbbbcc表示为3-a,4-b,2-c.那么问t串在s串中出现了 ...
- CodeForces 631D Messenger
$KMP$. $n=1$和$n=2$的时候可以单独计算.$n>2$时,可以拿字符和数字分别做一次匹配,然后扫描一遍判断一下就可以计算出答案了. #pragma comment(linker, & ...
- Codeforces Round #344 (Div. 2) D. Messenger kmp
D. Messenger 题目连接: http://www.codeforces.com/contest/631/problem/D Description Each employee of the ...
- 【18.40%】【codeforces 631D】Messenger
time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standa ...
- CF #344 D. Messenger KMP/Z
题目链接:http://codeforces.com/problemset/problem/631/D 给定两个压缩形式的字符串,如a3b5a4k7这样的形式 问A在B中出现次数. 分类讨论,如果A是 ...
- CodeForces 25E Test KMP
Description Sometimes it is hard to prepare tests for programming problems. Now Bob is preparing tes ...
- Codeforces 126B. Password (KMP)
<题目链接> 题目大意:给定一个字符串,从中找出一个前.中.后缀最长公共子串("中"代表着既不是前缀,也不是后缀的部分). 解题分析:本题依然是利用了KMP中next数 ...
- Codeforces 126B(kmp)
要点 头尾的最长相同只要一个kmp即可得,于是处理中间部分 扫一遍记录一下前缀的每个位置是否存在一个中间串跟它相同,见代码 如果当前没有,接着用Next数组去一找即可 #include <cst ...
随机推荐
- 使用optimizely做A/B测试
摘要: optimizaly是一个提供A/B测试服务的网站,可以可视化地在线编辑测试内容和目标,简单方便. 1. A/B测试简介 所谓 A/B 测试,简单来说,就是为同一个目标制定两个方案(比如两 ...
- Adroid: getExternalStorageDirectory 不一定是你想要的外部存储SdCard
前情提要:我的测试机是华为荣耀6,我装过一个16G的内存卡 因为要面试的需要,我的一个演示项目用的是android本地的WebService.然而写好的webService部署到本地上,应用怎么获取数 ...
- OCP prepare 20140701
1. rman的完全备份,和不完全备份 Oracle 数据库可以实现数据库不完全恢复与完全恢复.完全恢复是将数据库恢复到最新时刻,也就是无损恢复,保证数据库无丢失的恢复.而不完全恢复则是根据需要特意将 ...
- [Script]EBS里查看模块的版本、文件的版本信息【Z】
系统版本信息 装了哪些模块,以及版本信息 select 'Current Application Release: '||ver||' ('||bug||')' "Description&q ...
- NSString+URLEncoding
NSString+URLEncoding.h #import <Foundation/Foundation.h> @interface NSString(URLEncoding) - (N ...
- 通过innerHTML简化脚本
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&q ...
- spoj BRCKTS - Brackets 线段树
题目链接 给一个括号序列, 两种操作. 一种将某个位置的括号变反(左变右, 右变左), 第二种是询问这个括号序列是否合法. 线段树, 我们开两个数组lf, rg. 表示某个区间里面, 右边的左括号个数 ...
- lua学习笔记1
lua中调用c的函数 #include <stdio.h> #include <string.h> #ifdef __cplusplus extern "C" ...
- linux开机自动挂载NTFS-WINDOWS分区
1.安装ntfs-3g-2009.4.4.tgz 2.输入fdisk -l 看一下分区 由此可见:/dev/sda5,6,7 即是windows下的D,E,F盘(NTFS格式). 3.vim /etc ...
- UI设计中与字号有关的知识
在我们设计APP.设计前端页面时,免不了要和各种文字大小打交道.字体的大小有多种单位,不明究里的话使用起来很容易出问题.今天整理了这方面的东西做了个图片,方便查看. 图上的资料来自互联网,感谢大家的负 ...