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 ...
随机推荐
- linux shell脚本守护进程监控svn服务
最近搭建的svn服务不知道什么原因服务总是被关闭(如果你不知道怎么搭建svn可以参考linux下搭建svn版本控制软件),因此用shell脚本实现一个守护进程.用于监控svn服务是否启动,如果服务不在 ...
- 【转】windows下安装和调用curl的方法
本文转自:http://1316478764.iteye.com/blog/2100778 curl是利用URL语法在命令行方式下工作的开源文件传输工具.它支持很多协议:FTP, FTPS, HTTP ...
- Java for LeetCode 190 Reverse Bits
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- Java for LeetCode 050 Pow(x, n)
Implement pow(x, n). 解题思路: 直接使用乘法实现即可,注意下,如果n很大的话,递归次数会太多,因此在n=10和n=-10的地方设置一个检查点,JAVA实现如下: static p ...
- 【python】lxml查找属性为指定值的节点
假设有如下xml在/home/abc.xml位置 <A> <B id=" name="apple"/> <B id=" name= ...
- 【python】choice函数
来源:http://www.runoob.com/python/func-number-choice.html 描述 choice() 方法返回一个列表,元组或字符串的随机项. 语法 以下是 choi ...
- 【2016-07-11】Qt远程部署失败,提示"没有那个文件或目录"的解决方法
首先明确一下,这里的部署失败与网络连接.ssh/scp/sftp等无关. 一般出现在删除了远端上的可执行文件,而本地程序未做明显改动时远程部署执行的时候. Qt应用程序输出中的提示信息如下: 究其原因 ...
- css3学习总结4--CSS3背景
css3背景 1. background-size 2. background-origin 3. background-clip 示例: className { background:url(bg_ ...
- Fedora install chrome
1)下载chrome:chrome download,选择rpm版,下载地址:https://dl.google.com/linux/direct/google-chrome-stable_curre ...
- oracle 10g 学习之游标使用和异常介绍(11)
一.游标 1. 使用游标 要求: 打印出 80 部门的所有的员工的工资: salary: xxx declare --1. 定义游标 cursor salary_cursor is select sa ...