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=,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 回文的更多相关文章
- LeetCode[5] 最长的回文子串
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- 最长回文子串-LeetCode 5 Longest Palindromic Substring
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [LeetCode] Palindrome Pairs 回文对
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [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 ...
- [LeetCode] Shortest Palindrome 最短回文串
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
随机推荐
- [另开新坑] 算导v3 #26 最大流 翻译
26 最大流 就像我们可以对一个路网构建一个有向图求最短路一样,我们也可以将一个有向图看成是一个"流量网络(flow network)",用它来回答关于流的问题. Just as ...
- tcp ip detatils
tcp ip detatils 8.关于TCP协议,下面哪种说法是错误的()A.TCP关闭连接过程中,两端的socket都会经过TIME_WAIT状态B.对一个Established状态的TCP连接, ...
- Extjs中给同一个GridPanel中的事件添加参数的方法
Extjs中给同一个GridPanel中的事件添加参数的方法: this.isUse = new Ext.Action({ text:'启用', scope ...
- MFC 相关文件夹、文件操作
//关于文件(夹)操作,可以参考下SHFileOperation这个外壳函数,貌似可以显示进度条.以下没有使用SHFileOperation//删除一个文件夹下的所有内容void myDeleteDi ...
- Eclipse设置C++自动补全变量名快捷键Alt + /
使用快捷键:Alt+/ 要是还是有些场合不能提示,按照下列步骤 Window-Preferences-c/c++-Editor-Content Assist-Advanced 将未勾选的全部勾选
- [Android Pro] sqlite数据库的char,varchar,text,nchar,nvarchar,ntext的区别
reference : http://blog.csdn.net/xingfeng0501/article/details/7817121 1.CHAR.CHAR存储定长数据很方便,CHAR字段上的索 ...
- [Android Pro] 利用tcpdump和wireshark对android网络请求进行分析
一: tcpdump操作流程 1. 手机要有root权限 2. 下载tcpdump http://www.strazzere.com/android/tcpdump 3. adb push c:\ ...
- 强制JSP页面刷新,防止被服务器缓存(可用于静态include强制刷新)
对于jsp页面,为了防止页面被服务器缓存.始终返回同样的结果. 通常的做法是在客户端的url后面加上一个变化的参数,比如加一个当前时间. 我现在使用的方法是在jsp头部添加以下代码: <% ...
- 个人博客转移到CSDN
因个人博客编辑的需要,现将我的博客转移到CSDN,如果没有特别需要,以后会在CSDN上继续书写博客 关于Fresco的分析以及后续的相关分析,也会转移到CSDN 个人CSDN主页: http://bl ...
- 以普通用户登录 su root 用vncviewer:xxxxx 会报错!!exit 回到最初环境变的用户 问题解决!!!!
[root@ok IT-DOC]# vncviewer : TigerVNC Viewer - built May :: Copyright (C) - TigerVNC Team and many ...