ZOJ 3587 Marlon's String 扩展KMP
链接: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;
}
ZOJ 3587 Marlon's String 扩展KMP的更多相关文章
- ZOJ 题目3587 Marlon's String(KMP)
Marlon's String Time Limit: 2 Seconds Memory Limit: 65536 KB Long long ago, there was a coder n ...
- acdream1116 Gao the string!(扩展KMP)
今天是字符串填坑的一天,首先填的第一个坑是扩展KMP.总结一下KMP和扩展KMP的区别. 在这里s是主串,t是模式串. KMP可以求出的是以s[i]为结尾的串和 t前缀匹配的最长的长度.假如这个长度是 ...
- hdu3336 Count the string 扩展KMP
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- HDU-3336-Count the string(扩展KMP)
链接: https://vjudge.net/problem/HDU-3336 题意: It is well known that AekdyCoin is good at string proble ...
- [2019杭电多校第五场][hdu6629]string matching(扩展kmp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6629 题意求字符串的每个后缀与原串的最长公共前缀之和. 比赛时搞东搞西的,还搞了个后缀数组...队友一 ...
- 【string】KMP, 扩展KMP,trie,SA,ACAM,SAM,最小表示法
[KMP] 学习KMP,我们先要知道KMP是干什么的. KMP?KMPLAYER?看**? 正如AC自动机,KMP为什么要叫KMP是因为它是由三个人共同研究得到的- .- 啊跑题了. KMP就是给出一 ...
- KMP && Manacher && 扩展KMP整理
KMP算法: kmp示例代码: void cal_next(char *str, int *next, int len) { next[0] = -1;//next[0]初始化为-1,-1表示不存在相 ...
- 扩展KMP --- HDU 3613 Best Reward
Best Reward Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3613 Mean: 给你一个字符串,每个字符都有一个权 ...
- UVA5876 Writings on the Wall 扩展KMP
扩展KMP的简单题. #include<stdio.h> #include<string.h> #define maxn 51010 char s[maxn],t[maxn]; ...
随机推荐
- python web开发 框架 模板 MVC
我是跟着廖雪峰老师学习的,对于我这样的纯小白来说,跟着他的网站学习,简直是被妈妈抱在怀里一样无忧无虑,这样的学习本来没有记录下来的必要,但是由于我的粗心大意,经常会出现一些错误,所以我决定把这些错误记 ...
- Unity Shader (四)片段程序示例
1.环境光+漫反射+高光+点光源 Shader "Custom/Example_Frag_1" { properties { _MainColor(,,,) _Specular ...
- 内存,寄存器和cache的区别与联系
1. 寄存器是中央处理器内的组成部份.寄存器是有限存贮容量的高速存贮部件,它们可用来暂存指令.数据和位址.在中央处理器的控制部件中,包含的寄存器有指令寄存器(IR)和程序计数器(PC).在中央处理器的 ...
- 【Codeforces Round #422 (Div. 2) B】Crossword solving
[题目链接]:http://codeforces.com/contest/822/problem/B [题意] 让你用s去匹配t,问你最少需要修改s中的多少个字符; 才能在t中匹配到s; [题解] O ...
- struts2怎么返回一个字符串给jsp?(使用json)
我们都知道使用servlet时可以直接用PrintWriter对象的print方法来向页面传送一些字符串(可以是html标签和内容),然后在用RequestDispatcher来转向网页 虽Strut ...
- 菜鸟之webservice(一) 服务端搭建
首先说一下,为什么取名叫菜鸟之webservice,由于本人技术真的不咋滴,写博客仅仅是为了对所学知识的总结.webservice对于我来说一直都是高大上的感觉,一个java web和javase都没 ...
- CODE ---代码助手 (保存代码、搜代码、生成网页、自由界面)
四大功能 1 保存代码 2 搜索代码 3 生成网页 4 自由界面 www.gudianxiaoshuo.com
- HDU 5358 First One 数学+尺取法
多校的题,摆明了数学题,但是没想出来,蠢爆了,之前算了半天的s[i][j]的和,其实是积.其实比赛的时候我连log(s[i][j])+1是s[i][j]的位数都没看出来,说出来都丢人. 知道了这个之后 ...
- 如何搭建Eclipse +Apache Tomcat配置Java开发环境
Linux平台下如何搭建Eclipse +Apache Tomcat配置的Java开发环境 本文出自 "李晨光原创技术博客" 博客,请务必保留此出处http://chenguang ...
- Servlet doPost方法同时上传图片和传递参数
上传图片和传递参数 上传图片和文件属于enctype="multipart/form-data" form中加入enctype="multipart/form-data ...