题目链接:点击查看

题目大意:给出一个长度为 n 的字符串,问有多少 相交的回文子串对数

题目分析:背 PAM 模板的时候突然发现了一道模板题,于是顺手写了。。正难则反,可以先求出有多少个互不相交的回文子串对数,参考:HDU - 5157

然后用总的回文子串的匹配个数减去不相交的,就是相交的答案了

注意,这个题目卡了一下内存,需要用 vector 来写 trie 树

然后就是 2e6 的字符串最多有 4e12 个本质不同的子串,答案大概是 16e24 的样子,我懒得中间取模,就直接 __int128 最后再取一下模了

2020.11.16update

用这个题练了一下Palindrome Series优化dp,整体思路都是一样的,只不过设 f[ i ] 为以 i 为结尾的回文串前面有多少个可以与其匹配的答案

代码:

//#pragma GCC optimize(2)
//#pragma GCC optimize("Ofast","inline","-ffast-math")
//#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=2e6+100;char s[N],str[N];vector<pair<int,int>>trie[N];int len[N],sed[N],fail[N],k,n,last;LL dp[N];int newnode()
{k++;trie[k].clear();fail[k]=len[k]=sed[k]=0;return k;
}int get_fail(int x)
{while(str[n-len[x]-1]!=str[n])x=fail[x];return x;
}void insert(int x)
{str[++n]='a'+x;int cur=get_fail(last);int now=-1;for(int i=0;i<trie[cur].size();i++)if(trie[cur][i].first==x){now=trie[cur][i].second;break;}if(now==-1){now=newnode();len[now]=len[cur]+2;int temp=get_fail(fail[cur]),pos=0;for(int i=0;i<trie[temp].size();i++)if(trie[temp][i].first==x){pos=trie[temp][i].second;break;}fail[now]=pos;sed[now]=sed[fail[now]]+1;trie[cur].emplace_back(x,now);}last=now;
}__int128 C(__int128 n)//return C(n,2)
{return n*(n-1)/2;
}void init()
{n=0;k=-1;newnode();newnode();fail[1]=fail[0]=1;len[1]=-1;last=0;
}int main()
{
#ifndef ONLINE_JUDGE
//  freopen("data.in.txt","r",stdin);
//  freopen("data.out.txt","w",stdout);
#endif
//  ios::sync_with_stdio(false);init();int n;scanf("%d",&n);scanf("%s",s+1);for(int i=1;i<=n;i++){insert(s[i]-'a');dp[i]=dp[i-1]+sed[last];}init();__int128 ans=0;for(int i=n;i>=1;i--){insert(s[i]-'a');ans+=dp[i-1]*sed[last];}printf("%d\n",(int)((C(dp[n])-ans)%51123987));return 0;
}

Palindrome Series优化

//#pragma GCC optimize(2)
//#pragma GCC optimize("Ofast","inline","-ffast-math")
//#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=2e6+100;const int mod=51123987;char s[N],str[N];vector<pair<int,int>>trie[N];int len[N],sed[N],fail[N],anc[N],diff[N],k,n,last;int dp[N],f[N],g[N];int newnode()
{k++;trie[k].clear();fail[k]=len[k]=sed[k]=0;return k;
}int get_fail(int x)
{while(str[n-len[x]-1]!=str[n])x=fail[x];return x;
}void trans(int i)
{dp[i]=(dp[i-1]+sed[last])%mod;for(int j=last;j>1;j=anc[j]){g[j]=dp[i-len[anc[j]]-diff[j]];if(diff[j]==diff[fail[j]])g[j]=(g[j]+g[fail[j]])%mod;f[i]=(f[i]+g[j])%mod;}
}void insert(int x)
{str[++n]='a'+x;int cur=get_fail(last);int now=-1;for(int i=0;i<trie[cur].size();i++)if(trie[cur][i].first==x){now=trie[cur][i].second;break;}if(now==-1){now=newnode();len[now]=len[cur]+2;int temp=get_fail(fail[cur]),pos=0;for(int i=0;i<trie[temp].size();i++)if(trie[temp][i].first==x){pos=trie[temp][i].second;break;}fail[now]=pos;sed[now]=sed[fail[now]]+1;trie[cur].emplace_back(x,now);diff[k]=len[k]-len[fail[k]];anc[k]=diff[k]==diff[fail[k]]?anc[fail[k]]:fail[k];}last=now;trans(n);
}void init()
{n=0;k=-1;newnode();newnode();fail[1]=fail[0]=1;len[1]=-1;last=0;
}int main()
{
#ifndef ONLINE_JUDGE
//  freopen("data.in.txt","r",stdin);
//  freopen("data.out.txt","w",stdout);
#endif
//  ios::sync_with_stdio(false);init();int n;scanf("%d",&n);scanf("%s",s+1);for(int i=1;i<=n;i++)insert(s[i]-'a');LL sum=1LL*dp[n]*(dp[n]-1)/2%mod;for(int i=1;i<=n;i++)sum=(sum-f[i]+mod)%mod;printf("%lld\n",sum);return 0;
}

CodeForces - 17E Palisection(回文自动机/Palindrome Series优化dp)相关推荐

  1. CodeForces - 906E Reverses(回文自动机+Palindrome Series优化dp)

    题目链接:点击查看 题目大意:给出两个字符串 s 和 t,每次可以在字符串 s 中选择数个不相交的字串进行反转,问最少需要反转多少次,可以使得字符串 s 和 t 相等,输出最小反转次数以及方案 题目分 ...

  2. CodeForces - 932G Palindrome Partition(回文自动机+Palindrome Series优化dp)

    题目链接:点击查看 题目大意:给出一个长度为偶数的字符串,问将其分割成 k 个子串记为 a[ 1 ] , a[ 2 ] ... a[ k ] ,且满足 a[ i ] == a[ k - i + 1 ] ...

  3. 2019ICPC(沈阳) (回文自动机+Palindrome Series优化dp)

    无从追溯的一道题目.. 题目大意:给出一个字符串 s,要求在 s 中选出3个互不相交的回文子串,求长度之和的最大值 题目分析:考虑 n * n 的 dp,dp[ i ][ k ] 为 s[ 1 : i ...

  4. SPOJ - IITKWPCE Let us play with strings(回文自动机+Palindrome Series优化dp)

    题目链接:点击查看 题目大意:给出一个长度为 n 的字符串,问最少拆分成多少个连续的子串,使得每个子串都是一个回文串 题目分析:dp[ i ] 代表 s[ 1 : i ] 的前缀最少可以拆分成多少个连 ...

  5. 怎么判断一个字符串的最长回文子串是否在头尾_回文自动机入门

    缘起 回文自动机(Palindrome auto machine PAM,有些地方称之为回文树)是回文问题的大杀器~  本文使用一道很简单的题目入门这个精巧的数据结构. hdu 2163 Palind ...

  6. CodeForces - 1326D2 Prefix-Suffix Palindrome (Hard version)(马拉车/回文自动机)

    题目链接:点击查看 题目大意:给出一个字符串,求出截取前缀和后缀后拼接而成的最长回文串,前缀和后缀不能相交 题目分析:题意很简单,思路也不难想,读完题后我尝试性的看了看样例,发现前缀和后缀拼接后如果能 ...

  7. 回文串问题的克星——Palindrome Tree(回文树)/Palindrome Automaton(回文自动机)学习小记

    前言 最近B组有一道我不会的题,赶紧学习. 简介 我们知道,Manacher算法可以在 O(n) O ( n ) O(n)的时间内求出以每个位置为中心的最长回文串(虽然我昨天还不知道Manacher算 ...

  8. 「HDU6599 I Love Palindrome String」 - 回文自动机

    HDU 6599 I Love Palindrome String tags:回文自动机 题意 让你求有多少个 \([l,r]\) 满足 \(s[l,r]\) 和 \(s\left[l,\frac{l ...

  9. HDU-6599 I Love Palindrome String 杭电第二次多校赛(Manacher+回文自动机)

    HDU-6599 I Love Palindrome String 杭电第二次多校赛(Manacher+回文自动机) 我的博客:https://acmerszq.cn 原题链接:http://acm. ...

最新文章

  1. ValueError: cannot convert to ‘int64‘-dtype NumPy array with missing values. Specify an appropriate
  2. 分享一个简单的功能集网站
  3. JAVA反射+SOCKET实现远程方法调用
  4. mysql数据库高级查询笔记_MySQL数据库基础——高级查询
  5. 站在巨人的肩膀,2020我在使用和涉及到的开源项目
  6. Magento Add Fee or Discount to Order Totals
  7. eclipse生成boolean型变量的getter是is开头
  8. 【mysql优化 2】索引条件下推优化
  9. 三轴机械臂逆运动学解算(附代码)
  10. 假如时光倒流我会这么学java
  11. C语言 车辆出租管理系统
  12. 工具系列 | FPM进程管理器详解
  13. loopback接口的具体作用
  14. 华为桌面云虚拟机如何安装Ubuntu 20.04.3-live-server
  15. 华为笔试时发现golang ACM模式输入的一个坑
  16. 手机游戏战斗服务器没有响应,奇葩战斗家手游APP无法登陆怎么处理 处理方案一览...
  17. html版本的网站地图只适合,网站地图-乐云SEO优化知识
  18. MathType怎么把公式替换成图片
  19. Schlumberger.PIPESIM.2017.1.932.Win64 1DVD
  20. python自动轨迹绘制_python day 20 自动轨迹绘制

热门文章

  1. php 设置curl不超时时间,curl命令的超时时间
  2. ios nstimer实现延时_IOS_IOS开发代码分享之用nstimer实现倒计时功能,用nstimer实现倒计时功能,废话 - phpStudy...
  3. html插入javascript变量,javascript如何引用变量?
  4. java树形菜单_Java构建树形菜单
  5. leader选举的源码分析-quorumPeer.createElectionAlgorithm
  6. Java的Excel导出方案介绍
  7. 反射_Class对象功能_获取Method
  8. 算术运算符_四则与取模运算
  9. springSecurity源码分析-spring-security.xml文件配置
  10. 数据库-优化-pt-query-digest使用简介