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 ...
随机推荐
- 《ASP.NET MVC4 WEB编程》学习笔记------Entity Framework的Database First、Model First和Code Only三种开发模式
作者:张博出处:http://yilin.cnblogs.com Entity Framework支持Database First.Model First和Code Only三种开发模式,各模式的开发 ...
- 《ASP.NET1200例》统计网站访问量源代码
void Application_Start(object sender, EventArgs e) { //在应用程序启动时运行的代码 int count=0; ...
- MySQL自带information_schema数据库使用
MySQL的information_schema数据库是什么,有什么作用? 大家在安装或使用MYSQL时,会发现除了自己安装的数据库以外,还有一个 information_schema数据库.info ...
- 初识lua
转自:http://www.oschina.net/question/12_115993-- 两个横线是单行注释(译者注:这跟 SQL 一样) --[[ 增加两个 [ 和 ] 变成多行注释 我是多行注 ...
- FastReport安装说明(中文版)
FastReport安装说明(中文版) 内容列表 I. IntroductionI. 介绍II. Manual installing of the FastReport packagesII. 手动安 ...
- 【JAVA、C++】LeetCode 006 ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- UBUNTU中如何获得root权限
在终端中输入: sudo passwd root Enter new UNIX password: (在这输入你的密码) Retype new UNIX password: (确定你输入的密码) pa ...
- javascript输出AscII码扩展集中的字符
function test(){ var c=""; for(var i=1;i<65536;i++){ if((i%10)==0){ c+=i+':\t'+String.f ...
- javascript首尾反转字符
var my_str="Welcome to www.sharejs.com" var i=my_str.length; i=i-1; for (var x = i; x > ...
- WPF入口Application
1.WPF和 传统的WinForm 类似, WPF 同样需要一个 Application 来统领一些全局的行为和操作,并且每个 Domain (应用程序域)中只能有一个 Application 实例存 ...