zoukankan      html  css  js  c++  java
  • 1025

    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
    ",s,dp[0][l-1]);
    43         }
    44         return 0;
    45 }
    油!油!you@
  • 相关阅读:
    开发,从需求出发 &#183; 之四 春天在这里
    面向基于英特尔&#174; 架构的 Android* 的 CoCos2D
    js左右切换 选择年龄
    先序遍历创建二叉树,对二叉树统计叶子节点个数和统计深度(创建二叉树时#代表空树,序列不能有误)c语言
    [ACM] hdu 1251 统计难题 (字典树)
    设计模式:单例模式的三种创建方式及其各自的优缺点
    [Android 4.4.2] 泛泰A850 Mokee4.4.2 20140509 RC2.0 by syhost
    IA32 MMU paging初始化代码
    为Android开发人员定制的搜索引擎
    Android中Activity切换时共享视图元素的切换动画(5.0以上)
  • 原文地址:https://www.cnblogs.com/zzuli2sjy/p/5602452.html
Copyright © 2011-2022 走看看