试题描述
给定字符串,求它的回文子序列个数。回文子序列反转字符顺序后仍然与原序列相同。
例如字符串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=,f=;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-;
for(;isdigit(c);c=getchar()) x=x*+c-'';
return x*f;
}
const int maxn=,mod=;
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+,r)+dp(l,r-)-dp(l+,r-)+mod)%mod;
return ans=(dp(l+,r)+dp(l,r-)+)%mod;
}
int main() {
int T=read();
rep(,T) {
scanf("%s",s+);
int n=strlen(s+);
memset(f,,sizeof(f));
printf("Case #%d: %d\n",i,dp(,n));
}
return ;
}

递推的话要以右端点升序,左端点降序来进行(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=,f=;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-;
for(;isdigit(c);c=getchar()) x=x*+c-'';
return x*f;
}
const int maxn=,mod=;
int f[maxn][maxn];
char s[maxn];
int main() {
int T=read();
rep(,T) {
scanf("%s",s+);
int n=strlen(s+);
for(int j=;j<=n;j++) {
f[j][j]=;
for(int i=j-;i;i--)
if(s[i]==s[j]) f[i][j]=(f[i+][j]+f[i][j-]+)%mod;
else f[i][j]=(f[i+][j]+f[i][j-]-f[i+][j-]+mod)%mod;
}
printf("Case #%d: %d\n",i,f[][n]);
}
return ;
}

COJ559 回文的更多相关文章

  1. LeetCode[5] 最长的回文子串

    题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  2. 最长回文子串-LeetCode 5 Longest Palindromic Substring

    题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  3. [LeetCode] Longest Palindrome 最长回文串

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  4. [LeetCode] Palindrome Pairs 回文对

    Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...

  5. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  6. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  7. [LeetCode] Palindrome Linked List 回文链表

    Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time ...

  8. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  9. [LeetCode] Palindrome Partitioning II 拆分回文串之二

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

随机推荐

  1. 最长公共子串 NYOJ 36

    http://acm.nyist.net/JudgeOnline/problem.php?pid=36 最长公共子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 ...

  2. Linux下 ntp 时间同步服务ntpd 出现 the NTP socket is in use, exiting 解决

    [root@EPDDB log]# [root@EPDDB log]# ntpdate 10.154.8.200 6 Sep 09:35:09 ntpdate[30210]: the NTP sock ...

  3. Linux下提取IP至文件

    ifconfig | grep 'inet[^6]' | sed 's/^\s*//g' | cut -d ' ' -f2 > ips.txt 排除127开头的IP: ifconfig | gr ...

  4. Django的Context和RequestContext

    参考:http://www.dannysite.com/blog/38/ Django的模板渲染中,Context可以用来传递数据,一个Context是一系列变量和值的集合,它和Python的字典有点 ...

  5. extjs插件开发上传下载文件简单案例

    前台,extjs,框架,mybatis,spring,springMVC,简单的文件上传下载案例. 必要的jar包,commons-fileupload-1.3.1.jar,commons-io-2. ...

  6. 【python】pathlib库

    pathlib在python3.2以上开始默认支持,在python2.7中如果要使用需要安装 pip install pathlib pathlib更多参考资料:http://pathlib.read ...

  7. 电话连线(codevs 1003)

    题目描述 Description 一个国家有n个城市.若干个城市之间有电话线连接,现在要增加m条电话线(电话线当然是双向的了),使得任意两个城市之间都直接或间接经过其他城市有电话线连接,你的程序应该能 ...

  8. 改变服务器sshd 的22的端口

    [root@v01-svn-test-server ~]# vi /etc/ssh/sshd_config Port 22 Port 5001 #新增加5001端口给sshd,现在22,5001都是s ...

  9. C# Winform ListView使用

    以下内容均来自网上,个人收集整理,具体出处也难确认了,就没写出处了: 一.基本使用: listView.View = View.Details;//设置视图 listView.SmallImageLi ...

  10. poj 1236 scc强连通分量

    分析部分摘自:http://www.cnblogs.com/kuangbin/archive/2011/08/07/2130277.html 强连通分量缩点求入度为0的个数和出度为0的分量个数 题目大 ...