2011百校联动“菜鸟杯”程序设计公开赛 &&Powerful Incantation
来源:互联网 发布:mysql proxy sysbench 编辑:程序博客网 时间:2024/06/10 18:34
kmp暴力求解飘过,,,
#include<iostream>#include<string>#include<string.h>using namespace std;string a,b;int nex[6];void kmp(){ int n; n=b.size(); nex[0]=0; int i=0,j=1; while(j<n) { if(b[j]==b[i]) {nex[j]=i+1; i++;j++; } else { if(i>0) i=nex[i-1]; else nex[j++]=0; } }}int main(){ int Case; cin>>Case; while(Case--) { cin>>a>>b; memset(nex,0,sizeof(nex)); kmp(); int n=a.size(); int m=b.size(); int num=0; int i=0,j=0; while(i<n) { if(a[i]==b[j]) { if(j==m-1) {num++;i++;j=0;} else {i++;j++;} } else{ if(j>0) j=nex[j-1]; else i++; } } cout<<num<<endl; }return 0;}
直接方法:
#include<string.h>#include<cstdio>#include<iostream>using namespace std;int main(){ int Case; cin>>Case; while(Case--) { char a[1000005]; char b[6]; scanf("%s%s",a,b); int n=strlen(a); int m=strlen(b); bool flag; int num=0; for(int i=0;i+m-1<n;++i){ flag=true;for(int j=0;j<m;++j) if(a[i+j]!=b[j]) { flag=false;break;} if(flag) {num++;i+=m-1;} } cout<<num<<endl; }return 0;}
- 2011百校联动“菜鸟杯”程序设计公开赛 &&Powerful Incantation
- 2011百校联动“菜鸟杯”程序设计公开赛-解题报告
- 2011百校联动“菜鸟杯”程序设计公开赛
- 2011百校联动“菜鸟杯”程序设计公开赛&&Flip Game
- 2011百校联动“菜鸟杯”程序设计公开赛&&Magic Potion
- 2011百校联动“菜鸟杯”程序设计公开赛(现更新5道)
- 2011百校联动“菜鸟杯”程序设计公开赛&&Length of S(n)
- 2011百校联动“菜鸟杯”程序设计公开赛&&Bacon's Cipher
- 2011百校联动“菜鸟杯”程序设计公开赛&&KFC -Z+W
- 2011百校联动“菜鸟杯”程序设计公开赛&&Cover The Enemy
- HDU 4150 Powerful Incantation
- HDU 4150 Powerful Incantation
- hdu 4150 Powerful Incantation
- 【字符串】hdu4150 Powerful Incantation
- hdu 4150 Powerful Incantation
- Powerful Incantation(HDU 4150)
- 17.04.29 Powerful Incantation
- hdu 4150 Powerful Incantation 暴力||KMP
- 那些导致程序抽风的神秘原因
- 注入入门级
- gdb 调试 程序执行脚本信息
- 工作中的细节
- SQLITE3 读写二进制字段blob
- 2011百校联动“菜鸟杯”程序设计公开赛 &&Powerful Incantation
- membership入门学习(五)
- enum和Enum区别
- 详解Xcode 4发布程序图文并茂教程
- XCode 4.2(4.1)真机调试及生成IPA全攻略
- 管理员用户安装Office 2003 报错
- 正则表达式学习笔记
- UML中依赖和关联的区别
- Yii 中 CComponent,CEvent与Behavior 关系分析