COJ559 回文


试题描述

给定字符串,求它的回文子序列个数。回文子序列反转字符顺序后仍然与原序列相同。
例如字符串aba中,回文子序列为"a", "a", "aa", "b", "aba",共5个。
注意:内容相同位置不同的子序列算不同的子序列。

输入

第一行一个整数T,表示数据组数。
之后是T组数据,每组数据为一行字符串。

输出

对于每组数据输出一行,格式为"Case #X: Y",X代表数据编号(从1开始),Y为答案。答案对100007取模。

输入示例

5
aba
abcbaddabcba
12111112351121
ccccccc
fdadfa

输出示例

Case #1: 5
Case #2: 277
Case #3: 1333
Case #4: 127
Case #5: 17

其他说明

1 ≤ T ≤ 10
字符串长度 ≤ 1000

第一眼hash、sa、马拉车什么的就行了。

第二眼样例的答案怎么这么大?

第三眼发现子串可以不连续

第四眼发现N这么小

第五眼发现这是一道裸的DP

第六眼设计出状态f[i][j]表示[i,j]的回文子串数目

第七眼设计出转移

f[i][i]=1

当s[i]!=s[j]时,根据容斥原理f[i][j]=f[i+1][j]+f[i][j-1]-f[i-1][j-1]

当s[i]==s[j]时,答案还要加上f[i-1][j-1]+1即s[i]加入回文串首,s[j]加入回文串尾,即f[i][j]=f[i+1][j]+f[i][j-1]+1

记忆化搜索有些慢(203ms)

#include<cstdio>
#include<cctype>
#include<queue>
#include<cstring>
#include<algorithm>
#define rep(s,t) for(int i=s;i<=t;i++)
#define ren for(int i=first[x];i!=-1;i=next[i])
using namespace std;
inline int read() {
    int x=0,f=1;char c=getchar();
    for(;!isdigit(c);c=getchar()) if(c==‘-‘) f=-1;
    for(;isdigit(c);c=getchar()) x=x*10+c-‘0‘;
    return x*f;
}
const int maxn=1010,mod=100007;
int f[maxn][maxn];
char s[maxn];
int dp(int l,int r) {
    int& ans=f[l][r];
    if(l>=r) return !(l>r);
    if(ans) return ans;
    if(s[l]!=s[r]) return ans=(dp(l+1,r)+dp(l,r-1)-dp(l+1,r-1)+mod)%mod;
    return ans=(dp(l+1,r)+dp(l,r-1)+1)%mod;
}
int main() {
    int T=read();
    rep(1,T) {
        scanf("%s",s+1);
        int n=strlen(s+1);
        memset(f,0,sizeof(f));
        printf("Case #%d: %d\n",i,dp(1,n));
    }
    return 0;
}

递推的话要以右端点升序,左端点降序来进行(79ms)

#include<cstdio>
#include<cctype>
#include<queue>
#include<cstring>
#include<algorithm>
#define rep(s,t) for(int i=s;i<=t;i++)
#define ren for(int i=first[x];i!=-1;i=next[i])
using namespace std;
inline int read() {
    int x=0,f=1;char c=getchar();
    for(;!isdigit(c);c=getchar()) if(c==‘-‘) f=-1;
    for(;isdigit(c);c=getchar()) x=x*10+c-‘0‘;
    return x*f;
}
const int maxn=1010,mod=100007;
int f[maxn][maxn];
char s[maxn];
int main() {
    int T=read();
    rep(1,T) {
        scanf("%s",s+1);
        int n=strlen(s+1);
        for(int j=1;j<=n;j++) {
            f[j][j]=1;
            for(int i=j-1;i;i--)
                if(s[i]==s[j]) f[i][j]=(f[i+1][j]+f[i][j-1]+1)%mod;
                else f[i][j]=(f[i+1][j]+f[i][j-1]-f[i+1][j-1]+mod)%mod;
        }
        printf("Case #%d: %d\n",i,f[1][n]);
    }
    return 0;
}

时间: 2024-07-30 11:49:51

COJ559 回文的相关文章

16-最少回文数组

Splits the string 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 Hrdv is interested in a string,especially the palindrome string.So he wants some palindrome string. A sequence of characters is a palindrome if it is the same written forwards and backwards. Fo

最少回文串--牛客网(秋招备战专场三模)-C++方向

题目描述:一个字符串从左向右和从右向左读都完全一样则是回文串,给定一个字符串,问该字符串中的字符所能组成的最少的回文串的个数为多少 解题思路:如果一个字符出现的次数为偶数,则必能组成回文串,如果一个字符出现奇数次,只能自己组成回文串,题目中问最少的回文串数目,即求出现次数为奇数次的字符个数即可,定义a存储每个字符出现的次数,统计出现奇数次的字符的个数,即为输出 1 #include <iostream> 2 #include <string> 3 using namespace s

回文判断

一个整形数是否是回文 also leetcode 9 Palindrome Number要求空间复杂度O(1)按位判断一般是/和%的游戏,首先取首位 a/h (h是最接近a的10的次方,比如12321,h预计算出是10000), 再取末位a%10; 比较首位和末位是否相等,不等就返回false; 如图: 然后舍弃掉已经比较过的两个位数,从a中去掉首尾 12321 --> 232. a = a % h; // 去掉首 a = a /10; //去掉尾 h = 100; // 因为已经去掉了两位 如

判断一个数是否为回文数

#include <stdio.h> int is_palindromic(int num) {  char _old = num;  char _new = 0;  while (num)  {   _new = _new * 10 + (num % 10);   num = num / 10;  }  if (_new == _old)  {   return 1;  }  else  {   return 0;  } } int main() {  int num = 0;  scanf

判断一个字符串是否为回文字符串

#include <stdio.h> #include <assert.h> #include <string.h> int is_pal_str(const char *p) {  assert(p);  int len = strlen(p);  const char *start = p;  const char *end = p+len - 1;  while (start < end)  {   if (*start == *end)   {    st

LeetCode 9 Palindrome Number (回文数)

翻译 确定一个整数是否是回文数.不能使用额外的空间. 一些提示: 负数能不能是回文数呢?(比如,-1) 如果你想将整数转换成字符串,但要注意限制使用额外的空间. 你也可以考虑翻转一个整数. 然而,如果你已经解决了问题"翻转整数(译者注:LeetCode 第七题), 那么你应该知道翻转的整数可能会造成溢出. 你将如何处理这种情况? 这是一个解决该问题更通用的方法. 原文 Determine whether an integer is a palindrome. Do this without ex

要求循环输入一个数,判断是否为回文数

import java.util.Scanner; public class HuiWenShu { public static void main(String[] args) { Scanner input = new Scanner(System.in); char c = 'y'; //初始化c为y,为下面的循环做好准备 while(c == 'y'){ while(c == 'y'){ System.out.println("请随意输入一个大于三位的奇位数"); //回文数属

编程之美2015 #2 回文字符序列

题目: 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 给定字符串,求它的回文子序列个数.回文子序列反转字符顺序后仍然与原序列相同.例如字符串aba中,回文子序列为"a", "a", "aa", "b", "aba",共5个.内容相同位置不同的子序列算不同的子序列. 输入 第一行一个整数T,表示数据组数.之后是T组数据,每组数据为一行字符串. 输出 对于每组数据输出一行,格式为&

HDU 3068 最长回文 (manacher算法)

最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9188    Accepted Submission(s): 3159 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. 回文就是正反读都是一样的字符串,如aba, abba等 Input 输入有多组