题目链接

首先想到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的更多相关文章

  1. Codeforces 631D Messenger【KMP】

    题意: 给定由字符串块(字符及连续出现的个数)组成的字符串t,s,求t串中有多少个s. 分析: KMP 这题唯一需要思考的地方就是如何处理字符串块.第一想到是把他们都展开然后进行KMP,可是展开后实在 ...

  2. CodeForces 631D Messenger —— (kmp的应用)

    这题是一个kmp的应用,思路是有,但是代码实现能力太弱,细节考虑不全,敲了很长时间才AC.. 题意:字符串用如下的方法表示,例如aaabbbbcc表示为3-a,4-b,2-c.那么问t串在s串中出现了 ...

  3. CodeForces 631D Messenger

    $KMP$. $n=1$和$n=2$的时候可以单独计算.$n>2$时,可以拿字符和数字分别做一次匹配,然后扫描一遍判断一下就可以计算出答案了. #pragma comment(linker, & ...

  4. Codeforces Round #344 (Div. 2) D. Messenger kmp

    D. Messenger 题目连接: http://www.codeforces.com/contest/631/problem/D Description Each employee of the ...

  5. 【18.40%】【codeforces 631D】Messenger

    time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standa ...

  6. CF #344 D. Messenger KMP/Z

    题目链接:http://codeforces.com/problemset/problem/631/D 给定两个压缩形式的字符串,如a3b5a4k7这样的形式 问A在B中出现次数. 分类讨论,如果A是 ...

  7. CodeForces 25E Test KMP

    Description Sometimes it is hard to prepare tests for programming problems. Now Bob is preparing tes ...

  8. Codeforces 126B. Password (KMP)

    <题目链接> 题目大意:给定一个字符串,从中找出一个前.中.后缀最长公共子串("中"代表着既不是前缀,也不是后缀的部分). 解题分析:本题依然是利用了KMP中next数 ...

  9. Codeforces 126B(kmp)

    要点 头尾的最长相同只要一个kmp即可得,于是处理中间部分 扫一遍记录一下前缀的每个位置是否存在一个中间串跟它相同,见代码 如果当前没有,接着用Next数组去一找即可 #include <cst ...

随机推荐

  1. VB.NET数据库编程基础教程

    关键词:作者罗姗   众所周知,VB.NET自身并不具备对数据库进行操作的功能,它对数据库的处理是通过.NET FrameWork SDK中面向数据库编程的类库和微软的MDAC来实现的.其中,ADO. ...

  2. poj1323--贪心算法

    题意:一群人打牌包括你,每人出一张牌,谁最大,谁就算赢一局,问你最少能赢几局? 给出人数N,每人的牌数M,及你的牌. 分析:1.这题需比较大小,就像我们打牌时要将牌排序以便出牌,显然要先将手上的牌进行 ...

  3. 制作与使用静态链接库(.lib)文件

    (一)制作.lib文件 (1)打开vs,选择“新建项目”,选择“Visual C++“,选择”Win32 控制台应用程序“. (2)点击”确定“,点击”下一步“,设置如下 (3)点击”完成“,然后就可 ...

  4. 游戏基础元素之精灵——Cocos2d-x学习历程(九)

    1.创建精灵 在实际使用中,精灵是由一个纹理创建的.在不加任何设置的情况下,精灵就是一张显示在屏幕上的图片.通常精灵置于层下,因此我们首选在层的初始化方法中创建精灵,设置属性,并添加到层中. 有多种方 ...

  5. 项目中怎么去掉tomcat的猫

    其实很简单,需要在登录页和显示内容的主页,将自己的favicon.ico 导入,并在登录页和主页将其引入即可. 例如: 接着在登录页引入  <link rel="shortcut ic ...

  6. Python封装的访问MySQL数据库的类及DEMO

    # Filename:mysql_class.py # Author:Rain.Zen; Date: 2014-04-15 import MySQLdb class MyDb: '''初始化[类似于构 ...

  7. [LeetCode]题解(python):146-LRU Cache

    题目来源: https://leetcode.com/problems/lru-cache/ 实现一个LRU缓存.直接上代码. 代码(python): class LRUCache(object): ...

  8. cv2.imread BGR模式

    openCV 的cv2.imread()导入图片时是BGR通道顺序,这与Matplotlib的显示,或者读取图片的通道不同,如果需要可以转换为RGB模式,以下代码显示不同之处,但BGR在许多地方使用, ...

  9. python 连接数据库-设置oracle ,mysql 中文字符问题

    import cx_Oracle import MySQLdb def conn_oracle(): cnn = cx_Oracle.connect('用户名','密码','ip:端口号/数据库') ...

  10. [转]iOS UIAppearance使用详解

    在iOS 5以前,自定义原生控件的外观并没有原生支持,因此开发人员感觉很麻烦.开发人员经常面临的问题是修改一个控件所有实例的外观.解决这个问题的正确方法是重写一遍控件.但由于这么做非常费时,一些开发人 ...