zoukankan      html  css  js  c++  java
  • UVa 10617 Again Palindrome 字符串dp

    Problem I

    Again Palindromes

    Input: Standard Input

    Output: Standard Output

    Time Limit: 2 Seconds

     

    A palindorme is a sequence of one or more characters that reads the same from the left as it does from the right. For example, ZTOT andMADAM are palindromes, but ADAM is not.

    Given a sequence S of N capital latin letters. How many ways can one score out a few symbols (maybe 0) that the rest of sequence become a palidrome. Varints that are only  different by an order of scoring out should be considered the same.

    Input

    The input file contains several test cases (less than 15). The first line contains an integer T that indicates how many test cases are to follow.

    Each of the T lines contains a sequence S (1≤N≤60). So actually each of these lines is a test case.

     

    Output

    For each test case output in a single line an integer – the number of ways.

     

    Sample Input                             Output for Sample Input

    3

    BAOBAB

    AAAA

    ABA

    22

    15

    5


    Russian Olympic Camp

    ----------------------

    忽然发现我的字符串dp弱成渣啊。。。

    f[i][j]表示从i到j的回文串个数。

    当i!=j时,f[i][j]=f[i+1][j]+f[i][j-1]-f[i+1][j-1];从i到j的回文数=删除左边的字符后的回文数+删除右边字符后的回文数-重复部分

    当i==j时,f[i][j]=f[i+1][j]+f[i][j-1]-f[i+1][j-1]+f[i+1][j-1]+1;从i到j的回文数=删除左边的字符后的回文数+删除右边字符后的回文数-重复部分+保留ij后的回文数+只留下ij的回文数即1

    --------------------------

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    int T;
    char s[111];
    long long f[111][111];
    int n;
    
    long long dfs(int l,int r)
    {
        if (f[l][r]!=-1) return f[l][r];
        if (r-l<0) return 0;
        if (s[l]==s[r])
        {
            f[l][r]=dfs(l+1,r)+dfs(l,r-1)+1;
        }
        else
        {
            f[l][r]=dfs(l+1,r)+dfs(l,r-1)-dfs(l+1,r-1);
        }
        return f[l][r];
    }
    
    int main()
    {
        cin>>T;
        while (T--)
        {
            memset(f,-1,sizeof(f));
            cin>>(s+1);
            n=strlen(s+1);
            long long ans=dfs(1,n);
            cout<<ans<<endl;
        }
        return 0;
    }
    





  • 相关阅读:
    dnn5.5.1的配置
    The Auto option has been disabled as the DotNetNuke Application cannot connect to a valid SQL Server database
    DNN常用的几种页面跳转(EditUrl和Globals.NavigateURL)
    动态生成ASP.NET按钮时要注意的一个问题
    Visual C#实现Windows信使服务
    浏览器滚动条的参数总结
    AJAX实现无刷新三联动下拉框
    c#.net常用的小函数和方法集
    利用OWC生成统计图表(代码+注释)
    ASP.NET之精通弹出窗口
  • 原文地址:https://www.cnblogs.com/cyendra/p/3226393.html
Copyright © 2011-2022 走看看