Gym - 101981M:(南京) Mediocre String Problem(回文树+exkmp)
#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
char S[maxn],T[maxn];
struct PT
{
struct in{
int dep,fail,len,son[];
}p[maxn];
int cnt,last;
void init()
{
//memset(p,0,sizeof(p));
cnt=last=;p[].dep=p[].dep=;
p[].fail=p[].fail=;
p[].len=; p[].len=-;
}
int add(int c,int n)
{
int np=last;
while(S[n]!=S[n--p[np].len]) np=p[np].fail;
if(!p[np].son[c]){
int v=++cnt,k=p[np].fail; p[v].len=p[np].len+;
while(S[n]!=S[n-p[k].len-]) k=p[k].fail;
p[v].fail=p[k].son[c];
p[np].son[c]=v; //这一句放前面会出现矛盾,因为np可能=k
p[v].dep=p[p[v].fail].dep+;
}
last=p[np].son[c];
return p[last].dep;
}
}Tree;
int N,M,num[maxn],Next[maxn],extand[maxn]; ll ans;
void getnext(){// next[i]: 以第i位置开始的子串与T的公共前缀长度
int i,length=strlen(T+);
Next[]=length;
for(i=;i+<length&&T[i+]==T[i+];i++);
Next[]=i;
int a=; //!
for(int k=;k<=length;k++){//长度+1,位置-1。
int p=a+Next[a]-, L=Next[k-a+];
if(L>=p-k+){
int j=(p-k+)>?(p-k+):;//中断后可能是负的
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(){
memset(Next,,sizeof(Next));
getnext();
int Slen=strlen(S+),Tlen=strlen(T+),a=;
int MinLen=Slen>Tlen?Tlen:Slen;
while(a<MinLen&&S[a+]==T[a+]) a++;
extand[]=a; a=;
for(int k=;k<=Slen;k++){
int p=a+extand[a]-,L=Next[k-a+];
if(L>=p-k+){
int j=(p-k+)>?(p-k+):;
while(k+j<=Slen&&j+<=Tlen&&S[k+j]==T[j+]) j++;
extand[k]=j;a=k;
}
else extand[k]=L;
}
}
int main()
{
scanf("%s%s",S+,T+);
N=strlen(S+); M=strlen(T+);
reverse(S+,S+N+); Tree.init();
rep(i,,N) num[i]=Tree.add(S[i]-'a',i);
getextand();
rep(i,,N) ans+=(ll)num[i-]*extand[i];
printf("%lld\n",ans);
return ;
}
不知道写什么。。。
Gym - 101981M:(南京) Mediocre String Problem(回文树+exkmp)的更多相关文章
- 【HDU5421】Victor and String(回文树)
[HDU5421]Victor and String(回文树) 题面 Vjudge 大意: 你需要支持以下操作: 动态在前端插入一个字符 动态在后端插入一个字符 回答当前本质不同的回文串个数 回答当前 ...
- HDU 5421 Victor and String(回文树)
Victor and String Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/262144 K (Java/Othe ...
- 杭电多校HDU 6599 I Love Palindrome String (回文树)题解
题意: 定义一个串为\(super\)回文串为: \(\bullet\) 串s为主串str的一个子串,即\(s = str_lstr_{l + 1} \cdots str_r\) \(\bullet\ ...
- HDU - 5421:Victor and String (回文树,支持首尾插入新字符)
Sample Input 6 1 a 1 b 2 a 2 c 3 4 8 1 a 2 a 2 a 1 a 3 1 b 3 4 Sample Output 4 5 4 5 11 题意:多组输入,开始字符 ...
- HDU - 5157 :Harry and magic string (回文树,求多少对不相交的回文串)
Sample Input aca aaaa Sample Output 3 15 题意: 多组输入,每次给定字符串S(|S|<1e5),求多少对不相交的回文串. 思路:可以用回文树求出以每个位置 ...
- 2018南京网络赛 - Skr 回文树
题意:求本质不同的回文串(大整数)的数字和 由回文树的性质可知贡献只在首次进入某个新节点时产生 那么只需由pos和len算出距离把左边右边删掉再算好base重复\(O(n)\)次即可 位移那段写的略微 ...
- Mediocre String Problem (2018南京M,回文+LCP 3×3=9种做法 %%%千年好题 感谢"Grunt"大佬的细心讲解)
layout: post title: Mediocre String Problem (2018南京M,回文+LCP 3×3=9种做法 %%%千年好题 感谢"Grunt"大佬的细 ...
- ACM-ICPC2018南京赛区 Mediocre String Problem
Mediocre String Problem 题解: 很容易想到将第一个串反过来,然后对于s串的每个位置可以求出t的前缀和它匹配了多少个(EXKMP 或者 二分+hash). 然后剩下的就是要处理以 ...
- 2014-2015 ACM-ICPC, Asia Xian Regional Contest G The Problem to Slow Down You 回文树
The Problem to Slow Down You Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjud ...
随机推荐
- Java获取路径(getResource)
package com.zhi.test; public class PathTest { public static void main(String[] args) { System.out.pr ...
- Win10系列:JavaScript 动画1
在应用程序中使用动画会使应用显得更加生动,进而给用户带来良好的视觉效果.例如,当用户将某个项添加到列表时,新项不会立即出现在列表中,而是采用动画形式到达相应位置,并且列表中的其他项也采用动画形式移动到 ...
- js 中class选择器,addClass,removeClass,hasClass,toggleClass,getByClass
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...
- jsp下载excel文件
jsp下载excel文件的的实现方法很多,今天也遇到这个问题,乱敲了一阵,终于搞定了,记下来和朋友们分享吧. 假设需要下载excel文件的jsp页面名为:down.jsp 对应的后台action名为: ...
- 【转载】OpenCV 摄像头控制
参考:[OpenCV] -- 简单摄像头操作 - 代码人生 - 博客频道 - CSDN.NET http://blog.csdn.net/qiurisuixiang/article/details/8 ...
- Python3 线程/进程池 concurrent.futures
python3之concurrent.futures一个多线程多进程的直接对接模块,python3.2有线程池了 Python标准库为我们提供了threading和multiprocessing模块编 ...
- 玩linux就是不断的踩坑,踩坑。最近的坑。xpath firefox兼容问题,抓取表格。
最近在抓取一个页面表格时发现,用firefox提取的xpath,不能用,仔细分析后,发现是提取的xpath多了一个tbody标签.在xpath路径中删掉这段就好了. last_A5='/html/bo ...
- 《Python》re模块补充、异常处理
一.re模块 1.match方法 import re # match 验证用户输入的内容 ret = re.match('\d+', 'hhoi2342ho12ioh11') print(ret) # ...
- [转]如何远程连接运行OpenGL/Cuda 等GPU程序
发现一篇神文,解决了困扰许久的远程桌面OpenGL/GPU 等问题... 原地址在这:http://www.tanglei.name/how-to-run-gpu-programs-using-rem ...
- 第三节 java 函数
1.函数的定义 1.方法就是一段可重复调用的代码段 2.方法的格式1: 访问修饰符 返回值类型 方法名(参数类型 参数1,参数类型 参数2){ //权限访问修饰符 : public protected ...