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 ...
随机推荐
- /etc/profile、~/.bash_profile、~/.bashrc和/etc/bashrc
文件 引用关系 执行时间 影响用户 使用场景 /etc/profile 开机执行 所有用户 所有用户.重启生效 ~/.bash_profile 引用~/.bashrc 用户登录时执行 当前用户 当 ...
- js地址多选实现,居住地,户口,职业,行业多选2
需求是根据点击一个按钮 实现动态添加数据,所以每个数据都有一个地址多选, 以下是效果实现及部分关键代码,相关js相关文件在 http://www.cnblogs.com/zhan1995/p/848 ...
- linux command useradd
Linux command useradd [Purpose] Learning linux command useradd to create a new user or update ...
- Vue + Element UI 实现权限管理系统 (功能组件封装)
组件封装 为了避免组件代码的臃肿,这里对主要的功能部件进行封装,保证代码的模块化和简洁度. 组件结构 组件封装重构后,试图组件结构如下图所示 代码一览 Home组件被简化,包含导航.头部和主内容三个组 ...
- 网络基础协议part 1
1.计算机与计算机之间如何进行联系? 两个独立的计算机是无法进行交流的,如同人一样,如果没有语言的存在就不能正常的交流.而在计算机领域,互联网协议就如同一门计算机与计算机交流的语言.但是为了全世界人们 ...
- wpf自定义控件中使用自定义事件
wpf自定义控件中使用自定义事件 1 创建自定义控件及自定义事件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 2 ...
- jenkins部署java项目在本地(三)
(1)新建maven构建的java项目 pom.xml的配置 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns ...
- eclipse项目环境搭建(做了好多遍,老是忘记,以此文帮助记忆)
今天把eclipse的环境搭建好(不能用myeclipse好忧伤). 要求: 1.在svn上管理项目,要下载svn插件. 2.是web所以要用到tomcat插件. 3.将项目运行起来. 流程: ecl ...
- spin lock自旋锁 双链表操作(多线程安全)(Ring0)
通过spin lock自旋锁 ,为每个链表都定义并初始化一个锁,在需要向该链表插入或移除节点时不使用前面介绍的普通函数,而是使用如下方法: ExInterlockedInsertHeadList(&a ...
- 强化学习3-蒙特卡罗MC
之前讲到强化学习可以用马尔科夫决策过程来描述,通常情况下,马尔科夫需要知道 {S A P R γ},γ是衰减因子,那为什么还需要蒙特卡罗呢? 首先什么是蒙特卡罗? 蒙特卡罗实际上是一座赌城的名字,蒙 ...