In mathematics, a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. For example, the sequence <A, B, D> is a subsequence of <A, B, C, D, E, F>. 
(http://en.wikipedia.org/wiki/Subsequence)

Given a string S, your task is to find out how many different subsequence of S is palindrome. Note that for any two subsequence X = <S x1, S x2, ..., S xk> and Y = <Sy1, S y2, ..., S yk> , if there exist an integer i (1<=i<=k) such that xi != yi, the subsequence X and Y should be consider different even if S xi = S yi. Also two subsequences with different length should be considered different.

InputThe first line contains only one integer T (T<=50), which is the number of test cases. Each test case contains a string S, the length of S is not greater than 1000 and only contains lowercase letters.OutputFor each test case, output the case number first, then output the number of different subsequence of the given string, the answer should be module 10007.Sample Input

4
a
aaaaa
goodafternooneveryone
welcometoooxxourproblems

Sample Output

Case 1: 1
Case 2: 31
Case 3: 421
Case 4: 960

大致题意是给定一个字符串,求回文子序列个数,最后的答案要%10007

首先定义f数组f[l][r]表示l~r区间的回文子序列个数,f[i][i]=1;

显然 根据容斥原理 :f[l][r]=f[l][r-1]+f[l+1][r]-f[l+1][r-1]  (因为中间的个数会算两遍);

然后,我们考虑s[l]==s[r]的情况,如果这两个位置相等,那么l+1 ~ r-1这个区间的所有子序列,都可以加入l和r这两个元素,构成一个新的回文子序列,除此之外 l和r这两个元素也可以构成一个回文子序列

注意减的时候取模+要加模

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
#include<cmath>
const int maxn=1e5+;
const int mod=;
typedef long long ll;
using namespace std;
char str[];
ll dp[][];
int main()
{
int T;
int cnt=;
cin>>T;
for(int t=;t<=T;t++)
{
scanf("%s",str+);
int len=strlen(str+);
memset(dp,,sizeof(dp));
for(int t=;t<=len;t++)
{
dp[t][t]=;
if(t<len&&str[t]==str[t+])
{
dp[t][t+]=;
}
else
{
dp[t][t+]=;
}
}
for(int l=;l<=len;l++)
{
for(int j=;j+l<=len;j++)
{
int r=j+l;
if(str[j]==str[r])
{
dp[j][r]=(dp[j+][r]+dp[j][r-]+)%mod;
}
else
dp[j][r]=(dp[j][r-]+dp[j+][r]-dp[j+][r-]+mod)%mod;
}
}
printf("Case %d: %lld\n",cnt++,dp[][len]); }
return ;
}

最新文章

  1. php-cgi not found
  2. B/S结构的流程简单概述
  3. linux(centos)搭建SVN服务器
  4. java.lang.UnsatisfiedLinkError: C:\apache-tomcat-8.0.21\bin\tcnative-1.dll: Can&#39;t load IA 32-bit .dll on a AMD 64-bit platform
  5. 一个简单的Lua解释器
  6. JavaScript中的继承(原型链)
  7. ajax局部更新
  8. 解决php与IIs的冲突
  9. [luogu P2647] 最大收益(贪心+dp)
  10. df、du、fdisk:Linux磁盘管理三板斧的使用心得(转载)
  11. 想弄清楚LIBEVENT和EPOLL之类的关系,不要因为走得太急而迷失了方向
  12. dtree的使用
  13. maven项目文件乱码问题
  14. 使用工具来提升Android开发效率
  15. JDK自带的监控分析工具JConsole
  16. lua 数据类型
  17. prometheus监控示例
  18. android listView功能简介
  19. JQuery Mobile - 解决页面点击时候,页眉和页脚消失问题!
  20. jmeter 线程组之间的参数传递(加密接口测试三)

热门文章

  1. 005_针对于go语言中速率限制的思考
  2. 一道 3 行代码的 Python面试题,我懵逼了一天
  3. 又一个小而美的Java Web框架: Solon!
  4. 利用maven的MyBatis Generator 插件自动创建代码
  5. java web应用启动报错:Caused by: java.lang.ClassNotFoundException: ServletContext
  6. 2020-07-26:如何用 socket 编程实现 ftp 协议?
  7. 2020-07-08:mysql只有一个表a,什么情况下会造成死锁,解决办法是什么?
  8. C#LeetCode刷题之#594-最长和谐子序列​​​​​​​​​​​​​​(Longest Harmonious Subsequence)
  9. CPU:别再拿我当搬砖工了!
  10. windows10永久激活工具 新版win10激活工具(绝对有效的永久激活工具)