zoj 3643 Keep Deleting
来源:互联网 发布:中国邮政网络银行 编辑:程序博客网 时间:2024/05/16 05:38
思路:KMP+堆栈,很巧妙的思路,用栈记录下每一次匹配成功时候的位置,当完全匹配成功的时候,就弹出相应程度的堆栈内容,原来一直错的地方在于,每次放到堆栈里的是陪陪失败后的位置,但是那样的话会有重复的情况,所以应该果断换一种算法。
/*########################################################################## File Name: k.cpp# Author: CaoLei# Created Time: 2015/7/14 16:11:49#########################################################################*/#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#include <set>#include <stack>#include <queue>#include <map>using namespace std;#define MAX(x,y) (((x)>(y))?(x):(y))#define MIN(x,y) (((x)<(y))?(x):(y))#define N 500010#define pi acos(-1.0)#define inf 100000000typedef long long ll;typedef unsigned long long ull;char a[300],b[550000];int fnext[300],n,m;void build(){ int i,j=0; fnext[1]=0; for(i=2;i<=n;i++){ while(j>0&&a[j+1]!=a[i]) j=fnext[j]; if(a[j+1]==a[i]) j++; fnext[i]=j; }}int kmp(){ int ans=0; stack<int> s; int j=0,tmp; for(int i=1;i<=m;i++){ while(j>0&&a[j+1]!=b[i]){ j=fnext[j]; } if(a[j+1]==b[i]) j++; s.push(j); if(j==n){ for(int cnt=1;cnt<=n;cnt++){ s.pop(); } if(s.empty()) j=0; else { j=s.top(); } ans++; } } if(!s.empty()) s.pop(); return ans;}int main(){ //freopen("in.txt","r",stdin); while(~scanf("%s%s",a+1,b+1)){ n=strlen(a+1); m=strlen(b+1); memset(fnext,0,sizeof(fnext)); build(); printf("%d\n",kmp()); } return 0;}
0 0
- zoj 3643 Keep Deleting
- ZOJ 3643 - Keep Deleting
- ZOJ 3643:Keep Deleting
- zoj 3643 Keep Deleting
- ZOJ-3643-Keep Deleting【字符串】
- ZOJ3643 Keep Deleting
- 字符串处理Keep Deleting
- zoj 2186 Keep on Truckin'
- ZOJ 2186 Keep on Truckin'
- zoj 2186 Keep on Truckin'
- ZOJ 2186 Keep on Truckin'
- ZOJ 2186 Keep on Truckin'
- ZOJ - 2186 Keep on Truckin'
- ZOJ Problem Set - 2186 Keep on Truckin'
- keep
- ZOJ Problem Set - 2186 Keep on Truckin'【模拟】
- Deleting Logs
- Deleting Clients
- 一个简单RPC框架是如何炼成的(I)——开局篇
- NGINX引入线程池 性能提升9倍
- POJ 1125 Stockbroker Grapevine //Floyd算法
- Linux下安装配置MongoDB数据库
- OpenGL画线和直方图笔记
- zoj 3643 Keep Deleting
- Chrome谷歌浏览器如何禁用JavaScript
- Nginx 定义自己的 http 模块
- 安卓开班自我介绍
- Rectangle Area
- 4.3堆积面积图
- hdu2199
- ubuntu 重新安装mysql
- Objective-C中的@property和@synthesize用法