zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    springboot和springcloud版本对应关系
    nexus安装包下载
    centos7安装Redis的踩坑之旅
    搭建本地Spring Initializr服务器
    ElasticSearch数据查看插件elasticsearch-head
    ELK学习历程
    如何使用ob函数输出静态html文件
    微信开发之获取jsapi_ticket
    static_关键字
    static关键字_1
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7489570.html
Copyright © 2011-2022 走看看