首页 > 其他分享 >C20220725T3 回文

C20220725T3 回文

时间:2022-08-31 12:34:00浏览次数:56  
标签:ss int char C20220725T3 inline c11 回文

给定字符串 \(s\) ,求 \(s_{l,r}\) 中回文串个数。多组询问, \(|s|\leq 5000\) , \(T\leq10^5\) 。


首先介绍 \(O(n\times T)\) 的离谱做法(竟然没卡掉),先跑 \(Manachar\) ,然后暴力查询 \([l,r]\) 的回文串数量,最后用一个数组记录下来(防1 5000数据)即可。

然后是正解,首先处理出 \(g[i][j]\) 表示 \(s_{i,j}\) 是否为回文串,然后推出 \(s_{i,j}\) 中回文串总个数 \(f[i][j]=f[i+1][j]+f[i][j-1]-f[i+1][j-1]+g[i][j]\) ,最后处理一下边界。(还是只有错误复杂度的代码)。

#include<bits/stdc++.h>
using namespace std;
struct IO{
    inline char read(){
        static const int IN_LEN=1<<18|1;
        static char buf[IN_LEN],*s,*t;
        return (s==t)&&(t=(s=buf)+fread(buf,1,IN_LEN,stdin)),s==t?-1:*s++;
    }
    template <typename _Tp> inline IO & operator >> (_Tp&x){
        static char c11,boo;
        for(c11=read(),boo=0;!isdigit(c11);c11=read()){
            if(c11==-1)return *this;
            boo|=c11=='-';
        }
        for(x=0;isdigit(c11);c11=read())x=x*10+(c11^'0');
        boo&&(x=-x);
        return *this;
    }
    inline void push(const char &c) {
    	putchar(c);
	}
	template <class T>
	inline void write(T x){
		if (x < 0) x = -x, push('-');
		static T sta[35];
		T top = 0;
		do {
			sta[top++] = x % 10, x /= 10;
		} while (x);
		while (top) push(sta[--top] + '0');
	}
	template <class T>
	inline void write(T x, char lastChar){
		write(x),push(lastChar);
	}
}io;

int t,slen;
int p[200005];
char s[100005];
char ss[200005];
pair<int,int> Q;
int anss[5005][5005];

int main(){
	scanf("%s",s+1);
	slen=strlen(s+1);
	ss[0]='!',ss[2*slen+1]='#',ss[2*slen+2]='?';
	for(int i=1;i<=slen;++i){
		ss[2*i-1]='#';
		ss[2*i]=s[i];
	}
	int mx=0,id=0;
	for(int i=1;i<=2*slen+1;++i){
		p[i]=((i>=mx)?0:min(p[2*id-i],mx-i));
		while(ss[i-p[i]]==ss[i+p[i]])
			++p[i];
		if(i+p[i]-1>mx){
			mx=i+p[i]-1;
			id=i;
		}
	}
	io>>t;
	while(t--){
		int ans=0;
		int l,r;
		io>>l>>r;
		if(anss[l][r]!=0){
			printf("%d\n",anss[l][r]);
			continue;
		}
		for(int i=2*l;i<=2*r;++i){
			ans+=(min(p[i],min(i-2*l+2,2*r-i+2))/2);
		}
		anss[l][r]=ans;
		printf("%d\n",ans);
	}
}

标签:ss,int,char,C20220725T3,inline,c11,回文
From: https://www.cnblogs.com/zhouzizhe/p/16642661.html

相关文章

  • leetcode-中心扩散法-回文数
    /***<p>给你一个字符串<code>s</code>,找到<code>s</code>中最长的回文子串。</p>**<p>&nbsp;</p>**<p><strong>示例1:</strong></p>**<pre>*<str......
  • 回文自动机(回文树)学习笔记
    回文自动机(回文树)学习笔记前置知识建议提前学习Manacher算法和其他任何一种自动机,方便理解,不过不学问题应该也不大。定义回文自动机(PAM),也称回文树,是存储一个字符串......
  • 1616. 分割两个字符串得到回文串
    给你两个字符串 a和 b ,它们长度相同。请你选择一个下标,将两个字符串都在 相同的下标分割开。由 a 可以得到两个字符串: aprefix 和 asuffix ,满足 a=aprefix......
  • leetcode 647. Palindromic Substrings回文子串(中等)
    一、题目大意给你一个字符串s,请你统计并返回这个字符串中回文子串的数目。回文字符串是正着读和倒过来读一样的字符串。子字符串是字符串中的由连续字符组成的一......
  • 动态规划——leetcode5、最长回文子串
    1、题目描述:2、解题方法:动态规划动态规划解题步骤:1、确定状态最后一步:如果s[i,...,j]是回文子串,那么需要满足两个条件①s[i......
  • 一个字符串回文子串数量最大的排列 证明
    CF1063A一个字符串回文子串数量最大的排列证明Problem-1063A-Codeforces若将一个字符串任意排列,要使其中的回文子串数量最多,按字典序排序是一种方法。首先,在一个......
  • PAM(回文自动机/回文树)
    一个由小写字母构成的字符串\(s\),对于\(s\)的每个位置,求出以该位置结尾的回文子串个数。这个字符串被进行了加密,除了第一个字符,其他字符都需要通过上一个位置的答案来......
  • leetcode5-最长回文子串
    最长回文子串中心扩展法从中心向两侧扩展,分为两种情况:一种是奇数长度,一种是偶数长度,需要分开讨论。classSolution{publicStringlongestPalindrome(Strings)......
  • 区间DP の 题(内含 最长回文串,石子合并,删除字符串,乘积最大,释放囚犯)
    乘积最大  由于题目给定的是m,需要分解成m+1部分的乘积,不难想到乘号刚好是m个,那么该题就转化成了m个乘号的插入方式。  最优子结构分析:    设数字字符串为a1a......
  • [2016年NOIP普及组] 回文日期
    [2016年NOIP普及组]回文日期题目大意:用 8 位数字表示一个日期,其中,前 4 位代表年份,接下来 2 位代表月 份,最后 2 位代表日期,一个日期是回文的,当且仅当表示这个日......