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@
  • 相关阅读:
    SSRS 2012 创建ReportServer数据库失败: The RPC server is not listening. (Exception from HRESULT: 0x800706B3)
    关于SSIS包调用,把Execute out of Process 设成True后运行失败问题
    Mark:Insert, Update, and Delete Destination table with SSIS
    转SSIS 2012 pass values from child package to parent with project deployment model
    [转]SSIS Parameter和Variable的区别
    转SQL SERVER – SSIS Parameters in Parent-Child ETL Architectures – Notes from the Field #040
    简单的plsql to tsql ,游标拆解
    TSQL 根据表名生成UPDATE SELECT INSERT
    SPLIT CURSOR INTO ROWSET DML
    c/c++ tricks
  • 原文地址:https://www.cnblogs.com/zzuli2sjy/p/5602452.html
Copyright © 2011-2022 走看看