poj3461Oulipo【kmp】
2015-08-27 17:38
260 查看
大意:kmp
View Code
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int maxn = 1000005; int next[maxn]; void get(char *s) { int l = strlen(s); int j = 0, k = -1; next[0] = -1; while(j < l) { if(k == -1 || s[j] == s[k]) { next[++j] = ++k; } else { k = next[k]; } } } int kmp(char *s1, char *s2) { int l1 = strlen(s1), l2 = strlen(s2); int i = 0, j = 0; int sum = 0; get(s2); while(i < l1 && j < l2) { if(j == -1 || s1[i] == s2[j]) { i++; j++; } else { j = next[j]; } if(j == l2) { sum++; j = next[j]; } } return sum; } char s1[maxn], s2[maxn]; int main() { int t; scanf("%d",&t); while(t--) { scanf("%s",s1); scanf("%s",s2); // printf("%s %s\n",s1, s2); printf("%d\n", kmp(s2,s1)); } }
View Code
相关文章推荐
- swift笔记(3)
- JAVA问题总结之17-杨辉三角的实现
- logback logback.xml常用配置详解(三)
- org.apache.axis2.AxisFault: The AXIS engine could not find a target service to invoke! targetServic
- 大数据架构:flume-ng+Kafka+Storm+HDFS 实时系统组合
- android studio下使用SqlCipher
- 整合SSH框架所需的包
- WebDriverException: Message: Failed to navigate to http://10.1.96.59:9080解决办法
- Android 网络okHttp基础
- Swift Tutorial(swift 教程) 1: Welcome to Programming
- 【公告】C币系统升级,会议系统升级
- iOS开发-C语言-指针
- HttpURLConnection与HttpClient区别及联系
- java.lang.SecurityException: ConnectivityService: Neither user 10086 nor current process has android
- Hbase原理简析
- css animation 简写和参数
- iOS 与 js 交互
- Linux基础(一)安装Linux后简单系统优化
- 欢迎使用CSDN-markdown编辑器
- 资源配置