/**
题目:hdu6153
链接:http://acm.hdu.edu.cn/showproblem.php?pid=6153
题意:给定两个串,求其中一个串t的每个后缀在另一个串s中出现的次数乘以其长度之和。
思路:扩展kmp
先将两个字符串翻转过来。那么变成求t串每个前缀在s串出现的次数。
直接扩展kmp求出extend[i]表示s串[i,n-1]子串和t串的最长公共前缀。
那么s串从i开始和t串前缀有匹配的贡献为1+2+...+extend[i] = extend[i]*(extend[i]+1)/2; */
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
using namespace std;
typedef long long LL;
#define ms(x,y) memset(x,y,sizeof x)
typedef pair<int, int> P;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + ;
const int maxn = 1e6+;
char s[maxn], t[maxn];
int Next[maxn], extend[maxn];
int cnt[maxn];
LL ans;
void GetNext(char *T,int* next)
{
int a=;
int Tlen=strlen(T);
next[]=Tlen;
while(a<Tlen-&&T[a]==T[a+]) a++;
next[]=a;
a=;
for(int k=;k<Tlen;k++)
{
int p=a+next[a]-,L=next[k-a];
if((k-)+L>=p)
{
int j=(p-k+)>? p-k+:;
while(k+j<Tlen&&T[k+j]==T[j]) j++;
next[k]=j;
a=k;
}
else next[k]=L;
}
} void GetExtend(char *S,char *T,int* next,int* extend)
{
int a=;
GetNext(T,next);
int Slen=strlen(S);
int Tlen=strlen(T);
int MinLen=Slen<Tlen? Slen:Tlen;
while(a<MinLen&&S[a]==T[a]) a++;
extend[]=a;
a=;
for(int k=;k<Slen;k++)
{
int p=a+extend[a]-,L=next[k-a];
if((k-)+L>=p)
{
int j=(p-k+)>? p-k+:;
while(k+j<Slen&&j<Tlen&&S[k+j]==T[j]) j++;
extend[k]=j;
a=k;
}
else extend[k]=L;
}
}
int main()
{
int T;
cin>>T;
while(T--)
{
scanf("%s%s",s,t);
ms(cnt,);
int n = strlen(s);
int m = strlen(t);
reverse(s,s+n);
reverse(t,t+m);
GetExtend(s,t,Next,extend);
ans = ;
for(int i = ; i < n; i++){
ans = (ans+(LL)extend[i]*(extend[i]+)/)%mod;
}
printf("%lld\n",ans);
}
return ;
}

hdu6153 扩展kmp求一个字符串的后缀在另一个字符串出现的次数。的更多相关文章

  1. HDU 3613 Best Reward(扩展KMP求前后缀回文串)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分割 ...

  2. POJ 2752 (kmp求所有公共前后缀长度)

    <题目链接> <转载于> 题目大意:  给出一个字符串str,求出str中存在多少子串,使得这些子串既是str的前缀,又是str的后缀.从小到大依次输出这些子串的长度.即输出该 ...

  3. 【kmp+求所有公共前后缀长度】poj 2752 Seek the Name, Seek the Fame

    http://poj.org/problem?id=2752 [题意] 给定一个字符串,求这个字符串的所有公共前后缀的长度,按从小到达输出 [思路] 利用kmp的next数组,最后加上这个字符串本身 ...

  4. HDU-6153 A Secret 扩展KMP

    题意:求一个字符串的所有后缀在母串中的出现次数*后缀的长度的总和. 题目链接:http://acm.split.hdu.edu.cn/viewcode.php?rid=22147273 思路:先预处理 ...

  5. 扩展KMP题目

    hdu4333 /* 题意:字符串s[0..n-1],每次把最后一个字符放到前面,求形成的字符串比最初串分别小,相同,大于的个数 因为是为了练习扩展KMP所以肯定是扩展KMP, 为了循环方便,在后面复 ...

  6. A - A Secret -扩展KMP

    题目大意: 给你两个字符串A,B,现在要你求B串的后缀在A串中出现的次数和后缀长度的乘积和为多少. 题解: 扩展KMP模板题,将A和B串都逆序以后就变成了求前缀的问题了,扩展KMP求处从i位置开始的最 ...

  7. A - A Secret (扩展kmp)

    题目链接:https://cn.vjudge.net/contest/283743#problem/A 题目大意:给你字符串s1和s2,然后问你s2的每一个后缀在s1中出现的次数之和(可重叠). 具体 ...

  8. J - Clairewd’s message HDU - 4300(扩展kmp)

    题目链接:https://cn.vjudge.net/contest/276379#problem/J 感觉讲的很好的一篇博客:https://subetter.com/articles/extend ...

  9. HDU 6153 A Secret(扩展kmp)

    A Secret Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others)Total ...

随机推荐

  1. leetCode 86.Partition List(分区链表) 解题思路和方法

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  2. angularjs中使用$scope.$watch监控对象模型的变化

    如果对象模型发生变化时,可以使用$scope.$watch监控变化 <html ng-app="myApp"> <head> <title>an ...

  3. RDLC 动态列

    很久没有写博客了,关于动态列,国内很少资料有介绍动态列的,所想写点心得给哥们 啥是动态列呢?通常我们用存储过程时有列转行和行转列的做法,那么在RDLC 怎么支持呢?其实很简单,就是利用了RDLC的 C ...

  4. .NET设计规范一:设计规范基础

    http://www.cnblogs.com/yangcaogui/archive/2012/04/18/2447049.html 前一段时间阅读了“.NET设计规范”这本书,读后给我的感觉是规范对开 ...

  5. iOS-字符串拼接

    // // main.m // /* 将两个字符串 NSString * str1 = @"123"; NSString * str2 = @"abc"; 拼接 ...

  6. HttpClient 解说 (1) 基础

    前言 超文本传输协议(HTTP)或许是当今互联网上使用的最重要的协议了. Web服务,有网络功能的设备和网络计算的发展,都持续扩展了HTTP协议的角色,超越了用户使用的Web浏览器范畴.同一时候,也添 ...

  7. Python类,特殊方法, __getitem__,__len__, __delitem__

    特殊函数一般以__methodname__的形式命名,如:__init__(构造方法), __getitem__. __setitem__(subscriptable所需method), __deli ...

  8. [Swift A] - Using Swift with Cocoa and Objective-C--Mix and Match

    Swift与Objective-C的兼容能力允许你在同一个工程中同时使用两种语言.你可以用这种叫做“mix and match”的特性来开发基于混合语言的应用.使用Swfit的最新特性--“mix a ...

  9. js 替换json对象中的键名

      js 替换json对象中的键名 CreateTime--2018年3月30日15:38:50 Author:Marydon 情景描述: 有个json数组,现在需要将json对象中的key替换掉,值 ...

  10. Windows快捷键命令

    1. 新建一个文件夹: Ctrl + shift + N; 2. Windows 查看端口信息: 1.进入cmd窗口; 2.netstat -ano : 列出所有端口的情况.在列表中我们观察被占用的端 ...