Codeforces Round #344 (Div. 2) Messager KMP的应用
2016-03-19 14:18
363 查看
题目链接 http://codeforces.com/contest/631/problem/D, 大体思路是将头尾去掉, 然后匹配中间, 匹配好后在比较两边, 代码如下:
#include <bits/stdc++.h> using namespace std; typedef pair<char, long long> pcl; vector<pcl> s, t; int n, m; int next[200000+100]; int tpnum; char str[10]; void getnext(){ int i=1, j=0; next[1] = 0; while(i<t.size()-1){ if(j==0 || t[i]==t[j]){ ++i; ++j; next[i] = j; } else j = next[j]; } } long long KMP(){ long long res = 0; int i=0, j=1; while(i<s.size()){ if(j==0 || s[i]==t[j]) i++, j++; else j = next[j]; if(j==t.size()-1){ if(s[i].first==t[j].first&&s[i].second>=t[j].second&& s[i-t.size()+1].first==t[0].first && s[i-t.size()+1].second>=t[0].second) res++; j = next[j]; } } return res; } int main(){ // freopen("1", "r", stdin); scanf("%d%d", &n, &m); for(int i=0; i<n; i++){ scanf("%d-%s", &tpnum, str); int sz = s.size(); if(sz==0 || s[sz-1].first!=str[0]){ s.push_back((pcl){str[0], tpnum}); } else s[sz-1].second += tpnum; } for(int i=0; i<m; i++){ scanf("%d-%s", &tpnum, str); int sz = t.size(); if(sz==0 || t[sz-1].first!=str[0]){ t.push_back((pcl){str[0], tpnum}); } else t[sz-1].second += tpnum; } // for(int i=0; i<s.size(); i++) { // printf("%c %lld ", s[i].first, s[i].second); // } // printf("\n"); // for(int i=0; i<t.size(); i++) { // printf("%c %lld ", t[i].first, t[i].second); // } // printf("\n"); long long res = 0; if(t.size() == 1) { for(int i=0; i<s.size(); i++) if(t[0].first==s[i].first && s[i].second>=t[0].second) res += s[i].second-t[0].second+1; printf("%I64d\n", res); return 0; } if(t.size() == 2){ for(int i=0; i<s.size()-1; i++) if(t[0].first==s[i].first&&t[0].second<=s[i].second&& t[1].first==s[i+1].first&&t[1].second<=s[i+1].second) res++; printf("%I64d\n", res); return 0; } getnext(); printf("%I64d\n", KMP()); return 0; }
相关文章推荐
- OnAccountsUpdateListener
- 图片编辑canvas
- Redis快速入门
- 【HTML5】DOCTYPE声明
- python中学习有待解决的问题
- 关于IList、IQueryable、IEnumerable与Func、Expression的关系2-详解
- C#之文件管理
- 数据挖掘-MATLAB下svm小例子
- openssl学习篇之base64编码、解码;md5 摘要;sha1摘要;3des加密,解密;rsa算法
- 蓝桥杯 小朋友排队 (树状数组好题)
- python--软件、模块相关安装问题
- 第三次作业(二)————————————程序阅读
- [Eclipse] - Return change set from outgoing to unresolved
- java中位运算的一点研究
- rsync+sersync实现文件实时同步
- 内存分配
- JavaScript 中的 String.prototype.match() 方法
- Android Audio系统分析1(获得最小buffer部分)
- Educational Codeforces Round 9
- Python笔记-函数(20160319)