A题:Common Substrings(KMP应用)
注意:2号和3号get_next()函数中next[i]赋值时的区别,一个是0,一个是1,且不能互换
- #include<cstdio>
- #include<cstring>
- #include<iostream>
- using namespace std;
- const int maxn=1e5+;
- char ch[*maxn];
- char s[maxn],t[maxn];
- int T,next[*maxn];
- /*1.
- void get_next(char *s)
- {
- next[1]=0;
- //printf("%d\n",next[1]);
- int i=1,j=0;
- int slen=strlen(s);
- while(i<slen){
- if(j==0||s[i-1]==s[j-1]){
- i++,j++;
- next[i]=j;
- //printf("%d\n",next[i]);
- }
- else j=next[j];
- }
- }严蔚敏数据结构P83页代码
- */
- void get_next(char *s)
- {
- int slen=strlen(s);
- next[]=;
- for(int i=;i<slen;i++){
- int j=next[i];
- while(j&&s[i]!=s[j]) j=next[j];
- next[i+]=(s[i]==s[j])?j+:;
- }
- }
- /*3.
- void get_next(char *s)
- {
- next[0]=0,next[1]=0;
- //printf("%d\n",next[1]);
- int slen=strlen(s);
- for(int i=2;i<=slen;i++){
- int j=next[i-1];
- while(j&&s[i-2]!=s[j-1]) j=next[j];
- next[i]=(s[i-2]==s[j-1])?j+1:1;
- //printf("%d\n",next[i]);
- }
- }按照做数据结构笔试题的步骤一步一步推导
- */
- int main()
- {
- scanf("%d",&T);
- while(T--){
- cin>>s>>t;
- int slen=strlen(s);
- int tlen=strlen(t);
- for(int i=;i<tlen;i++) ch[i]=t[i];
- ch[tlen]='#';
- for(int i=;i<slen+tlen+;i++) ch[i+tlen+]=s[i];
- int now=slen+tlen+;
- ch[now]=;
- int ans=;
- memset(next,,sizeof(next));
- get_next(ch);
- while(next[now]>){
- ans++;
- now=next[now];
- }
- printf("%d\n",ans);
- }
- }
A题:Common Substrings(KMP应用)的更多相关文章
- dutacm.club_1087_Common Substrings_(KMP)_(结合此题通俗理解kmp的next数组)
1087: Common Substrings Time Limit:3000/1000 MS (Java/Others) Memory Limit:163840/131072 KB (Java/ ...
- poj 3415 Common Substrings(后缀数组+单调栈)
http://poj.org/problem?id=3415 Common Substrings Time Limit: 5000MS Memory Limit: 65536K Total Sub ...
- 【POJ3415】 Common Substrings(后缀数组|SAM)
Common Substrings Description A substring of a string T is defined as: T(i, k)=TiTi+1...Ti+k-1, 1≤i≤ ...
- 字符串(后缀数组):POJ 3415 Common Substrings
Common Substrings Description A substring of a string T is defined as: T(i, k)=TiTi+1...Ti+k-1, 1≤ ...
- 1087: Common Substrings (哈希)
1087: Common Substrings Time Limit:3000/1000 MS (Java/Others) Memory Limit:163840/131072 KB (Java/ ...
- POJ3415 Common Substrings —— 后缀数组 + 单调栈 公共子串个数
题目链接:https://vjudge.net/problem/POJ-3415 Common Substrings Time Limit: 5000MS Memory Limit: 65536K ...
- Common Substrings POJ - 3415 (后缀自动机)
Common Substrings \[ Time Limit: 5000 ms\quad Memory Limit: 65536 kB \] 题意 给出两个字符串,要求两个字符串公共子串长度不小于 ...
- POJ 3415 Common Substrings(后缀数组 + 单调栈)题解
题意: 给两个串\(A.B\),问你长度\(>=k\)的有几对公共子串 思路: 先想一个朴素算法: 把\(B\)接在\(A\)后面,然后去跑后缀数组,得到\(height\)数组,那么直接\(r ...
- hdu 1238 Substrings(kmp+暴力枚举)
Problem Description You are given a number of case-sensitive strings of alphabetic characters, find ...
随机推荐
- python2.0_s12_day9_mysql操作
mysql的基本语法: 1.数据库操作 show databases; create database 数据库名;如果想允许数据库可以写中文create database 数据库名 charset u ...
- Unity版本与虚拟现实头盔Deepoon大朋版本测试
一.看这里 Unity官方与OC runtime版本兼容性说明: https://developer.oculus.com/documentation/game-engines/latest/conc ...
- 用示例详解php连接数据库操作
首先数据库mydb有三个表: 1 info表 2 users表 3 sname表 首先先做一个登录主页面 login_1.php <!DOCTYPE html PUBLIC "- ...
- Vue基础---->vue-router的使用(一)
用 Vue.js + vue-router 创建单页应用,是非常简单的.使用 Vue.js 时,我们就已经把组件组合成一个应用了,当你要把 vue-router 加进来,只需要配置组件和路由映射,然后 ...
- Fluent Nhibernate Mapping for Sql Views
Views are mapped the same way tables are mapped except that you should put Readonly() in the mapping ...
- 优雅的go语言--入门篇
1.特点 1.静态类型,编译型的开源语言 2.脚本华的语法,支持多种编程范式(函数式&面向对象) 3.原生,给力的并发编程的支持 2.优势 1.脚本化的语法 2.静态类型+编译型,程序运行速度 ...
- ajax解决跨域方法(适用于自己写接口解决跨域)
原因是这样的:最近用PHP开发了一个网站,这个网站需要提供接口,接口开发完成之后,在本地进行请求,跨域测试. jsonp处理跨域和用PHP函数来处理跨域就不说了. 现在说的使用用 header 这个来 ...
- OneThink后台模型怎么玩?
OneThink 后台模型有个模型类型: 模型下——>设计——>表单显示分组(怎么玩?) 这个将会显示在:内——>发布文章内容的时候: 单选按钮: 内容模块显示: 枚举类型可以这样玩 ...
- 二进制状态压缩dp(旅行商TSP)POJ3311
http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Subm ...
- javascript飞机大战-----001分析
1.游戏引擎 首先要做飞机大战要考虑的是这个游戏被分成了哪几大部分?这样我们一块一块去做,特别清晰明了.那么接下来我们就简单的分析下飞机大战分成了哪几大部分 1.游戏引擎 2.英雄机 3.敌机 4.子 ...