CodeForces 631D Messenger
$KMP$。
$n=1$和$n=2$的时候可以单独计算。$n>2$时,可以拿字符和数字分别做一次匹配,然后扫描一遍判断一下就可以计算出答案了。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<bitset>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c=getchar(); x=;
while(!isdigit(c)) c=getchar();
while(isdigit(c)) {x=x*+c-''; c=getchar();}
} const int maxn=;
struct X { LL num; int a; }s[maxn],t[maxn];
int lens,lent,n,m;
char op[];
int nx[maxn],f[maxn],g[maxn];
LL a[maxn],b[maxn]; int len1,len2; void get_next()
{
int j=-,i=; nx[]=-;
while(i<len2)
{
if(j==-||b[i]==b[j]) i++, j++, nx[i]=j;
else j=nx[j];
}
} void kmp(bool x){
get_next();
int i=,j=;
while(i<len1){
if(j==-||a[i]==b[j]) i++ ,j++;
else j=nx[j];
if(j==len2)
{
if(x==) f[i-j+len2]=;
else g[i-j+len2]=;
}
}
} int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%s",op); int L=strlen(op);
int num=,sign;
for(int j=;j<=L-;j++) num=num*+op[j]-'';
sign=op[L-]-'a'+;
if(lens==||sign!=s[lens-].a) { s[lens].num=num; s[lens].a=sign; lens++; }
else s[lens-].num+=num;
} for(int i=;i<=m;i++)
{
scanf("%s",op); int L=strlen(op);
int num=,sign;
for(int j=;j<=L-;j++) num=num*+op[j]-'';
sign=op[L-]-'a'+;
if(lent==||sign!=t[lent-].a) { t[lent].num=num; t[lent].a=sign; lent++; }
else t[lent-].num+=num;
} if(lent==)
{
LL ans=;
for(int i=;i<lens;i++)
{
if(s[i].a!=t[].a) continue;
if(s[i].num<t[].num) continue;
ans=ans+s[i].num-t[].num+;
}
printf("%lld\n",ans);
}
else if(lent==)
{
LL ans=;
for(int i=;i<lens-;i++)
{
if(s[i].a!=t[].a||s[i+].a!=t[].a) continue;
if(t[].num>s[i].num||t[].num>s[i+].num) continue;
ans++;
}
printf("%lld\n",ans);
}
else
{
for(int i=;i<lens;i++) a[i]=s[i].a;
for(int i=;i<lent;i++) b[i]=t[i].a;
len1=lens,len2=lent; kmp(); memset(a,,sizeof a); memset(b,,sizeof b);
for(int i=;i<lens;i++) a[i]=s[i].num;
for(int i=;i<lent-;i++) b[i-]=t[i].num;
len1=lens; len2=lent-; kmp(); LL ans=;
for(int i=;i<lens;i++)
{
if(f[i+]==) continue;
if(g[i]==) continue;
if(s[i].num<t[lent-].num) continue;
if(s[i-lent+].num<t[].num) continue;
ans++;
}
printf("%lld\n",ans); }
return ;
}
CodeForces 631D Messenger的更多相关文章
- codeforces 631D. Messenger kmp
题目链接 首先想到kmp, 和普通的不一样的是,中间部分严格相等, 头和尾的字符相等但是数量可以不相等. 所以应该把子串的头和尾先去掉,然后对剩下的部分进行kmp. 子串长度为1或2要特别讨论. 不要 ...
- 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串中出现了 ...
- 【18.40%】【codeforces 631D】Messenger
time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standa ...
- codeforces 1288E. Messenger Simulator(树状数组)
链接:https://codeforces.com/contest/1288/problem/E 题意:序列p的长度为n,初始序列为1 2 3 4 ...n,然后有m次操作,每次指定序列中一个数移动到 ...
- 631D Messenger
题目大意 给你串s和t 但是每个串都被表示为多个二元组(x,y)表示字符x连续出现y次 问t在s中出现了多少次 分析 我们先将s和t每个串中二元组合并 即相邻两个二元组如果字符相等则将它们变为一个 特 ...
- Codeforces Round #344 (Div. 2) D. Messenger kmp
D. Messenger 题目连接: http://www.codeforces.com/contest/631/problem/D Description Each employee of the ...
- Codeforces Round #344 (Div. 2) D. Messenger (KMP)
D. Messenger time limit per test2 seconds memory limit per test512 megabytes inputstandard input out ...
- Educational Codeforces Round 80 (Rated for Div. 2) E. Messenger Simulator
可以推出 min[i]要么是i要么是1,当a序列中存在这个数是1 max[i]的话就比较麻烦了 首先对于i来说,如果还没有被提到第一位的话,他的max可由他后面的这部分序列中 j>=i 的不同数 ...
随机推荐
- 成C++应用程序世界------异常处理
一. 概述 C++自身有着很强的纠错能力,发展到现在,已经建立了比較完好的异常处理机制.C++的异常情况无非两种,一种是语法错误,即程序中出现了错误的语句,函数,结构和类,致使编译程序无法进行.还有一 ...
- Inno Setup connection to the database and create
原文 Inno Setup connection to the database and create Description: the first half of this program in I ...
- 通过localstorage和cookie实现记录文章的功能
我们在做页面的时候,会考虑记录用户曾经看过的文章的功能,并记录下来在页面中显示!但是在IE低版本的下是不支持localstorage的功能,只能采用cookie来代替本地存储的功能!实现的方法如下! ...
- leetcode[105] Construct Binary Tree from Inorder and Postorder Traversal
代码实现:给定一个中序遍历和后序遍历怎么构造出这颗树!(假定树中没有重复的数字) 因为没有规定是左小右大的树,所以我们随意画一颗数,来进行判断应该是满足题意的. 3 / \ 2 4 /\ / \1 6 ...
- Moq让单元测试变得更简单
[ASP.Net MVC3 ]使用Moq让单元测试变得更简单 前几天调查完了unity.现在给我的任务是让我调查Moq. 以下是自己找了资料,总结并实践的内容.如果有表述和理解错误的地方.恳请指正. ...
- Bootstrap3.0学习第八轮
Bootstrap3.0学习第八轮(工具Class) 前言 阅读之前您也可以到Bootstrap3.0入门学习系列导航中进行查看http://www.cnblogs.com/aehyok/p/34 ...
- [转]iOS Tutorial – Dumping the Application Heap from Memory
Source:https://blog.netspi.com/ios-tutorial-dumping-the-application-heap-from-memory/ An essential ...
- 程序处理数据库中值字段值为null的查询显示
1.如果你做了一个简单的注册界面,需要用户进行注册,但有些项是不必要填的,当用户完成注册时,数据库表中的相应字段的值会写入null,但如何将查询的字段的值null显示出来? 2.首先我们学习一下如何向 ...
- ios开发之路十一(ARC forbids explicit message send of 'autorelease'错误)
在ios中经常会遇到:ARC forbids explicit message send of 'autorelease' 或“ARC forbids explicit message send of ...
- <c:if>判断两个<c:forEach>里的数据是否相等
问题:两个<c:forEach>嵌套,里面循环的值和外面的值进行比较(里层里的PARENTID是否等于外层的ID),如果相等就显示. <c:forEach items="$ ...