1025 - The Specials Menu
Time Limit: 2 second(s) Memory Limit: 32 MB

Feuzem is an unemployed computer scientist who spends his days working at odd-jobs. While on the job he always manages to find algorithmic problems within mundane aspects of everyday life.

Today, while writing down the specials menu at the restaurant he's working at, he felt irritated by the lack of palindromes (strings which stay the same when reversed) on the menu. Feuzem is a big fan of palindromic problems, and started thinking about the number of ways he could remove letters from a particular word so that it would become a palindrome.

Two ways that differ due to order of removing letters are considered the same. And it can also be the case that no letters have to be removed to form a palindrome.

Input

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case contains a single word W (1 ≤ length(W) ≤ 60).

Output

For each case, print the case number and the total number of ways to remove letters from W such that it becomes a palindrome.

Sample Input

Output for Sample Input

3

SALADS

PASTA

YUMMY

Case 1: 15

Case 2: 8

Case 3: 11


PROBLEM SETTER: MUNTASIR MUZAHID CHOWDHURY
SPECIAL THANKS: JANE ALAM JAN (DATASET)
题意:给你一字符串,问你在这字符串里去掉一些字符构成回文串,有多少种方法;
思路:区间dp;
dp[i][j]表示在区间i,j中有多少个回文,然后状态转移方程dp[i][j]=dp[i][j-1]+dp[i+1][j];这里面有重复的这个时候分情况讨论,ans[i]==ans[j]的时候
我们有dp[i][j]=dp[i][j-1]+dp[i+1][j]+1,中间回文和两端构成新的回文,那么中间也就不需要删除了,并且加了问空时,就两端构成回文,当不等时
dp[i][j]=dp[i][j-1]+dp[i+1][j]-dp[i+1][j-1];
 1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<stdlib.h>
6 #include<math.h>
7 #include<queue>
8 #include<stack>
9 #include<vector>
10 using namespace std;
11 typedef long long LL;
12 LL dp[100][100];
13 char ans[100];
14 int main(void)
15 {
16 int i,j,k;
17 int s;
18 scanf("%d",&k);
19 for(s=1; s<=k; s++)
20 {
21 scanf("%s",ans);
22 int l=strlen(ans);
23 memset(dp,0,sizeof(dp));
24 for(i=0; i<l; i++)
25 {
26 for(j=i; j>=0; j--)
27 {
28 if(i==j)dp[j][i]=1;
29 else
30 {
31 if(ans[i]==ans[j])
32 {
33 dp[j][i]=dp[j][i-1]+dp[j+1][i]+1;
34 }
35 else
36 {
37 dp[j][i]= dp[j][i]=dp[j][i-1]+dp[j+1][i]-dp[j+1][i-1];
38 }
39 }
40 }
41 }
42 printf("Case %d: %lld\n",s,dp[0][l-1]);
43 }
44 return 0;
45 }

1025 - The Specials Menu的更多相关文章

  1. Lightoj 1025 - The Specials Menu (区间DP)

    题目链接: Lightoj 1025 - The Specials Menu 题目描述: 给出一个字符串,可以任意删除位置的字符,也可以删除任意多个.问能组成多少个回文串? 解题思路: 自从开始学dp ...

  2. Light OJ 1025 - The Specials Menu(动态规划-区间dp)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1025 题目大意:一串字符, 通过删除其中一些字符, 能够使这串字符变成回文串. ...

  3. Lightoj 1025 - The Specials Menu

    区间dp /* *********************************************** Author :guanjun Created Time :2016/6/30 23:2 ...

  4. Light OJ 1025 - The Specials Menu(区间DP)

    题目大意:     给你一个字符串,问有多少种方法删除字符,使得剩下的字符是回文串. 有几个规定: 1.空串不是回文串 2.剩下的字符位置不同也被视为不同的回文串.如:AA有三种回文串 A, A, A ...

  5. The Specials Menu LightOJ - 1025

    The Specials Menu LightOJ - 1025 题意:在给定的字符串中删去一些字符,使其成为回文串(不能全部都删).求方案数. 方法:常规的区间dp.ans[i][j]表示在i到j的 ...

  6. 【lightoj-1025】The Specials Menu(区间DP)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1025 [题目大意] 求一个字符串删去任意字符可以构成多少个不同的回文串 [分析 ...

  7. LightOJ1025 The Specials Menu(区间DP)

    给一个字符串,问有几种删字符的方式使删后的非空字符串是个回文串. 当然区间DP:dp[i][j]表示子串stri...strj的方案数 感觉不好转移,可能重复算了.我手算了"AAA" ...

  8. lightoj刷题日记

    提高自己的实力, 也为了证明, 开始板刷lightoj,每天题量>=1: 题目的类型会在这边说明,具体见分页博客: SUM=54; 1000 Greetings from LightOJ [简单 ...

  9. dp百题大过关(第一场)

    好吧,这名字真是让我想起了某段被某教科书支配的历史.....各种DP题层出不穷,不过终于做完了orz 虽然各种手糊加乱搞,但还是要总结一下. T1 Monkey Banana Problem    这 ...

随机推荐

  1. MapReduce03 框架原理InputFormat数据输入

    目录 1 InputFormat数据输入 1.1 切片与MapTask并行度决定机制 问题引出 MapTask并行度决定机制 Job提交流程源码 切片源码 1.2 FileInputFormat切片机 ...

  2. github小白的记录随笔

    此文章是基础本地安装好了git环境的新手小白. 进入您要上传项目的根路径,右键选择Git Bash Here. 输入命令: git init //初始化git仓库环境 git remote add o ...

  3. day03 部署NFS服务

    day03 部署NFS服务 NFS的原理 1.什么是NFS 共享网络文件存储服务器 2.NFS的原理 1.用户访问NFS客户端,将请求转化为函数 2.NFS通过TCP/IP连接服务端 3.NFS服务端 ...

  4. int是几位;short是几位;long是几位 负数怎么表示

    其实可以直接通过stm32的仿真看到结果:(这里是我用keil进行的测试,不知道这种方法是否准确) 从上面看, char是8位  short是4*4=16位  int是8*4=32位  long是8* ...

  5. mysql key与index的区别

    key包含了index, 而index没有key的功能. 1.key 是数据库的物理结构,它包含两层意义,一是约束(偏重于约束和规范数据库的结构完整性),二是索引(辅助查询用的).包括primary ...

  6. java内存管理的小技巧

    1,尽量使用直接量.     采用String str="hello"; 而不是 String str = new String("hello"): 2,使用S ...

  7. LocalDate计算两个日期相差天数

    import org.apache.commons.lang.StringUtils; import java.time.LocalDate; import java.time.ZoneId; imp ...

  8. RPC 框架

    RPC 谁能用通俗的语言解释一下什么是 RPC 框架? - 远程过程调用协议RPC(Remote Procedure Call Protocol) RPC就是要像调用本地的函数一样去调远程函数. 推荐 ...

  9. spring的核心容器ApplicationContext

    //bean.xml配置文件 <?xml version="1.0" encoding="UTF-8"?><beans xmlns=" ...

  10. <转>C/S架构分析

    系统架构师-基础到企业应用架构-客户端/服务器 开篇 上篇,我们介绍了,单机软件的架构,其实不管什么软件系统,都是为了解决实际中的一些问题,软件上为了更好的解决实际的问题才会产生,那么对于单机软 件的 ...