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 ...
随机推荐
- JavaScript 继承方式的实现
1.原型链继承 function superType(name){ this.name= 'milk'; } super.prototype.sayName=function(){ console.l ...
- MySQL函数笔记
MySQL函数笔记 日期函数 SELECT t1.xcjyrq, t1.* FROM view_sbxx t1 WHERE t1.syzt ; SELECT t1.xcjyrq, t1.* FROM ...
- textarea 输入框限制字数
在textarea标签中,只需要设置maxlength=”***”即可,但是在textarea标签中,IE9及IE9以下浏览器是不支持的,IE10.IE11则支持,估计后续的版本应该都会支持. 现在来 ...
- [LeetCode]题解(python):010-Regular Expression Matching
题目来源: https://leetcode.com/problems/regular-expression-matching/ 题意分析: 这道题目定义了两个正则表达式规则.’.’代表任意字符,’* ...
- Python的编码规范(PEP 8 & Google Python guide)
PEP 8 Python 代码规范整理 click here Goole Python 风格指南 中文版 click here 大家有取舍的看吧. 因为文章不是原创的,所以只贴地址,给大家造成麻烦了, ...
- 0x800a138f - JavaScript runtime error: Unable to get property 'asSorting' of undefined or null reference 错误原因以及解决办法
使用Jquery Datatables的时候也许会碰到这样的错误提示,当我们仔细的查找代码,发现引用的js文件,css文件均引用了,就是找不到他的问题所在. 这是从我们引用的js文件内部报的错. 这个 ...
- Linux(CentOS6.5) 开放端口,配置防火墙
打开配置文件 [root@localhost ~]# vi /etc/sysconfig/iptables 正确的配置文件 # Firewall configuration written by sy ...
- J2SE知识点摘记(二十四)
覆写hashCode() 在明白了HashMap具有哪些功能,以及实现原理后,了解如何写一个hashCode()方法就更有意义了.当然,在HashMap中存取一个键值对涉及到的另外一个方法为equa ...
- [问题解决] Could not update ICEauthority file /home/username/.ICEauthority
错误: Could not update ICEauthority file /home/username/.ICEauthority 发生场景: 虚拟机下的ubuntu server12.04 解决 ...
- Group Commit of Binary Log
160222 09:19:26 mysqld_safe Starting mysqld daemon with databases from /data01/mysql 2016-02-22 09:1 ...