• The Specials Menu LightOJ


    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

    3

    SALADS

    PASTA

    YUMMY

    Sample Output

    Case 1: 15

    Case 2: 8

    Case 3: 11

    注意:答案可能超过long long

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 #include<algorithm>
     5 using namespace std;
     6 typedef long long ll;
     7 
     8 ll n,dp[105][105];
     9 string S;
    10 
    11 void solve(int t){
    12     memset(dp,0,sizeof(dp));
    13     for(int i=n-1;i>=0;i--){
    14         for(int j=i;j<n;j++){
    15             dp[i][j]+=dp[i][j-1];
    16             dp[i][j]+=dp[i+1][j];
    17             if(S[i]==S[j]) dp[i][j]+=1;
    18             else dp[i][j]-=dp[i+1][j-1];
    19         }
    20     }
    21     printf("Case %d: %lld
    ",t,dp[0][n-1]);
    22 }
    23 
    24 int main()
    25 {   int kase;
    26     cin>>kase;
    27     for(int t=1;t<=kase;t++){
    28         cin>>S;
    29         n=S.size();
    30         solve(t);
    31     }
    32     return 0;
    33 }
  • 相关阅读:
    git添加本地项目到git
    GitLab项目迁移到Gerrit
    flask一些资料
    openldap sshkey & 用户自定义属性
    openldap复制
    openldap主机访问控制(基于用户组)
    openldap主机访问控制(基于ip)
    openldap自定义schema
    openldap主机访问控制(基于hostname)
    openldap权限sudo
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7489570.html
Copyright © 2020-2023  润新知