KMP
2016-04-22 16:27
225 查看
输入
第一行一个整数N,表示测试数据组数。
接下来的N*2行,每两行表示一个测试数据。在每一个测试数据中,第一行为模式串,由不超过10^4个大写字母组成,第二行为原串,由不超过10^6个大写字母组成。
其中N<=20
输出
对于每一个测试数据,按照它们在输入中出现的顺序输出一行Ans,表示模式串在原串中出现的次数。
第一行一个整数N,表示测试数据组数。
接下来的N*2行,每两行表示一个测试数据。在每一个测试数据中,第一行为模式串,由不超过10^4个大写字母组成,第二行为原串,由不超过10^6个大写字母组成。
其中N<=20
输出
对于每一个测试数据,按照它们在输入中出现的顺序输出一行Ans,表示模式串在原串中出现的次数。
#include "iostream" #include "algorithm" #include "string" using namespace std; int main() { int test; cin >> test; while(test--) { string t, s; cin >> t >> s; int next[10001]; int i = 0; int j = -1; int len1 = t.length(); int len2 = s.length(); next[0] = -1; while(i < len1) { if(j == -1 || t[i] == t[j]) next[++i] = ++j; else j = next[j]; } i = 0; j = 0; int ans = 0; while(i < len2) { if(j == -1 || s[i] == t[j]) { ++i; ++j; } else j = next[j]; if(j == len1) ans++; } cout << ans << endl; } return 0; }
相关文章推荐
- ViewPager+Fragment以及Fragment嵌套ViewPager
- 或许我们做错了,但绝非一无是处
- android自动刷新应用内存使用状态的方法
- Android SDK Android NDK Android Studio 官方下载地址
- 内存清理sql语句
- WPF异常:在“System.Windows.Markup.StaticResourceHolder”上提供值时引发了异常。
- CoreData的一些简单运用
- 你的孤独,虽败犹荣 摘抄
- 欢迎使用CSDN-markdown编辑器
- Java重载时的类型转换
- 【bzoj2002】[Hnoi2010]Bounce 弹飞绵羊
- Coco2d-x-2.x和3.x创建工程以及编译安卓注意事项
- 关于spring发email总结
- 利用Handler来实现UI线程的更新
- 括号配对(南阳理工2)栈
- 在Word中如何自动生成参考文献引用
- BP神经网络的网络带宽预测
- django/python日志logging 的配置以及处理
- 华为交换机板卡端口型号对照
- 快吧游戏盒官方下载2016 v4.0.5.6959 官方最新版