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 ...
随机推荐
- PHP日期格式转时间戳
PHP 提供了函数可以方便的将各种形式的日期转换为时间戳,该类函数主要是: strtotime():将任何英文文本的日期时间描述解析为时间戳. mktime():从日期取得时间戳. strtotime ...
- Java 7 的7个新特性
1.对集合类的语言支持:(??) 2.自动资源管理: 3.改进的通用实例创建类型推断:(??) 4.数字字面量下划线支持:(√) 5.switch中使用string:(√) 6.二进制字面量:(√) ...
- JS 保留小数点后面2位小数
1. 最笨的办法....... [我就怎么干的.........] function get(){ var s = 22.127456 + ""; var str = ...
- iOS 和Android中的正则表达式简单使用
ios 中需要使用NSRegularExpression类,NSTextCheckingResult类. 下面给出最基本的实现代码 NSRegularExpression *regex = [NSRe ...
- ShortestPath:Silver Cow Party(POJ 3268)
牛的聚会 题目大意:一群牛在一块农田的不同的点,现在他们都要去到同一个地方开会,然后现在从那个地方回到原来的位置,点与点之间的连线都是单向的,并且通过一个路径需要一定时间,问你现在哪只牛需要最多的时间 ...
- window.open()读取本地图片简单使用总结
最近做了一个项目,需要读取本地图片出来,问了一些人,感觉在数据库中存取路径比较合适,故做此方法. 后台查询出来的路径
- MFC 文件按行读写 CStdioFile
//写文件 CStdioFile file; file.Open("test.txt",CFile::modeCreate|CFile::modeReadWrite); file. ...
- How to Optimize Battery Health?
1. click on the battery icon from taskbar next to the date and time. 2. click "More power optio ...
- sysctl命令详解
个人一般sysctl -p 或sysctl -a比较多使用 sysctl配置与显示在/proc/sys目录中的内核参数.可以用sysctl来设置或重新设置联网功能,如IP转发.IP碎片去除以及源路由检 ...
- KVM中Linux虚拟机的硬盘添加方法
[root@cache01 ~]# df -hT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_roo ...