链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3587

题意:给出两个字符串S和T。S,T<=100000.拿出S的两个子串(能够重叠),将两个子串连接起来成为字符串T的方法有多少种。

思路:用扩展KMP求出S的从每位開始的子串与T的公共前缀,再将两个子串翻转,再用扩展KMP求出S反的从每位開始的子串与T反的公共前缀。找出当中和为T子串长度的S公共前缀和S反的公共前缀的数量,相乘为结果。

代码:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define eps 1e-8
#define INF 0x7fffffff
#define maxn 10005
#define PI acos(-1.0)
#define seed 31//131,1313
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
const int N = 101010;
int next_a[N],extand_a[N],next_c[N],extand_c[N];
void getnext(char *T,int *next,int *extand) // next[i]: 以第i位置開始的子串 与 T的公共前缀
{int i,length = strlen(T);next[0] = length;for(i = 0; i<length-1 && T[i]==T[i+1]; i++);next[1] = i;int a = 1;for(int k = 2; k < length; k++){int p = a+next[a]-1, L = next[k-a];if( (k-1)+L >= p ){int j = (p-k+1)>0?

(p-k+1) : 0; while(k+j<length && T[k+j]==T[j]) j++;// 枚举(p+1,length) 与(p-k+1,length) 区间比較 next[k] = j, a = k; } else next[k] = L; } } void getextand(char *S,char *T,int *next,int *extand) //s是母串,t是模式串 { memset(next,0,sizeof(next)); getnext(T,next,extand); int Slen = strlen(S), Tlen = strlen(T), a = 0; int MinLen = Slen>Tlen?

Tlen:Slen; while(a<MinLen && S[a]==T[a]) a++; extand[0] = a, a = 0; for(int k = 1; k < Slen; k++) { int p = a+extand[a]-1, L = next[k-a]; if( (k-1)+L >= p ) { int j = (p-k+1)>0? (p-k+1) : 0; while(k+j<Slen && j<Tlen && S[k+j]==T[j] ) j++; extand[k] = j; a = k; } else extand[k] = L; } } char a[100005],b[100005],c[100005],d[100005]; LL t_a[100005],t_c[100005]; void init() { memset(t_a,0,sizeof(t_a)); memset(t_c,0,sizeof(t_c)); } int main() { //freopen("1.txt","r",stdin); int T; scanf("%d",&T); while(T--) { init(); scanf("%s",a); scanf("%s",b); int len_a=strlen(a); for(int i=0;i<len_a;i++) c[i]=a[len_a-1-i]; c[len_a]='\0'; int len_b=strlen(b); for(int i=0;i<len_b;i++) d[i]=b[len_b-1-i]; d[len_b]='\0'; getextand(a,b,next_a,extand_a); getextand(c,d,next_c,extand_c); for(int i=0;i<len_a;i++) { t_a[extand_a[i]]++; t_c[extand_c[i]]++; } for(int i=len_a-1;i>=1;i--) { t_a[i]+=t_a[i+1]; t_c[i]+=t_c[i+1]; } LL ans=0; for(int i=1;i<len_b;i++) { ans+=t_a[i]*t_c[len_b-i]; } printf("%lld\n",ans); } return 0; }

转载于:https://www.cnblogs.com/gccbuaa/p/6991714.html

ZOJ 3587 Marlon#39;s String 扩展KMP相关推荐

  1. Zoj 3587 Marlon's String (KMP 字符串拼接 前缀出现次数)

    题意:给字符串S,T,找到所有的tetrad (a,b,c,d), Sa..b + Sc..d = T , a≤b and c≤d.也就是把T分成两段,这两段都由S中的子串组成的,求有多少中组合方式( ...

  2. ZOJ 3587 Marlon's String 扩展KMP

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3587 题意:给出两个字符串S和T,S,T<=100000.拿出 ...

  3. zoj 3587 Marlon's String(拓展KMP+dp)

    链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3587 题目大意: 给字符串S,T,   找到所有的tetrad ( ...

  4. [zoj 3587]Marlon's String[kmp]

    题意: Return the amount of tetrad (a,b,c,d) which satisfy Sa..b + Sc..d = T , a≤b and c≤d. 思路: 正反两次kmp ...

  5. ZOJ 3587 Marlon's String

    KMP,每匹配到一点就记录一下,然后用fail数组把已经匹配到的但是kmp没有记录的点加上.... Marlon's String Time Limit: 2000MS   Memory Limit: ...

  6. HDU - 6629 string matching(扩展KMP)

    题目链接:点击查看 题目大意:给出一个字符串 s 和一个暴力程序,用于求解 s 的每个后缀和原字符串的最长公共前缀,现在问一共需要执行多少次比较操作 题目分析:首先肯定不能暴力去模拟,时间复杂度n*n ...

  7. M - Mediocre String Problem( 扩展KMP + Manacher + 差分 )

    M - Mediocre String Problem( 扩展KMP + Manacher + 差分 ) 题意:给出一个串S,和一个串T. 要求 从S串中取一个子串,后面接上T串的一个前缀 组成一个结 ...

  8. Mediocre String Problem(马拉车+扩展KMP)

    文章目录 [Mediocre String Problem](https://codeforces.com/gym/101981) 题目大意 解题思路 代码 Mediocre String Probl ...

  9. Gym - 101981 Problem M. Mediocre String Problem (扩展KMP + Manacher)

    Problem M. Mediocre String Problem 题目链接:https://vjudge.net/problem/Gym-101981M 题目大意:给出两个串S,T,从S中选择 i ...

最新文章

  1. noclassdeffounderror java,从终端运行Java文件时出现java.lang.NoClassDefFoundError
  2. 转载|网络编程中阻塞式函数的底层逻辑
  3. pandas学习笔记四之读取写入文件
  4. vm的红帽linux怎样安装教程,vmware10怎么安装linux_redhat7系统安装教程
  5. 前端类名优秀命名例子_这是一篇需要花费你15分钟阅读的干货!浅谈前端工程化...
  6. python期末题目_Python期末复习题必考
  7. 怎样用一个3升的杯子和一个5升的杯子装出4升水来(杯子没有刻度)?
  8. 64位内核第三讲,Windbg的使用.以及命令
  9. SQLSERVER 清除链接历史记录
  10. C程序设计语言(第2版)简单读书笔记
  11. js简单实现div宽度匀速增加/减小
  12. Julia: 读出目录下所有文件
  13. python简单代码示例-python3简单代码示例
  14. h3c 链路聚合测试_H3CSE学习之链路聚合
  15. spring整合WebService入门详解
  16. 以下是一段歌词,请从这段歌词中统计出朋友出现的次数。 这些年一个人,风也过,雨也走,有过泪,有过错, 还记得坚持甚么,真爱过才会懂,会寂寞会回首,终有梦终有你在心中。 朋友一生一起走,那些日子不再
  17. linu安装mysql5.7
  18. 如何实现廣州南方学院校园网WiFi连接的高效性
  19. 微信小程序获取地理位置信息
  20. 什么是架构即代码( Infrastructure As Code)

热门文章

  1. 【把视频逐帧转换成图片】
  2. 耐看的《银元时代生活史》
  3. 计算机系统数据保存期限,哪种存储器存储数据时间长?
  4. Integer对象的大小比较
  5. 浙江计算机专业技术考试大纲,浙江省高校计算机等级考试大纲(三级)
  6. tplink android管理软件,tplink路由器app下载
  7. 劫持Linux idle进程做点自己的计算任务
  8. CorelDRAW版本限制使用关闭永久禁止联网登录弹窗口错误修复教程
  9. mfc实现c语言的注释,C语言学习:标识符、关键字、注释、表达式和语句
  10. 半导体器件物理【21】PN结 —— 载流子分布、正偏反偏