• 1040 Longest Symmetric String dp


    Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given Is PAT&TAP symmetric?, the longest symmetric sub-string is s PAT&TAP s, hence you must output 11.

    Input Specification:

    Each input file contains one test case which gives a non-empty string of length no more than 1000.

    Output Specification:

    For each test case, simply print the maximum length in a line.

    Sample Input:

    Is PAT&TAP symmetric?
    

    Sample Output:

    11
    我的错误代码
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #define inf 105
    using namespace std;
    int main()
    {
        string s;
        int n,dp[inf][inf],maxn=0;
        getline(cin,s);
        n=s.size();
        fill(dp[0],dp[0]+inf*inf,0);
        for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
        {
            if(i==j)
            dp[i][j]=1;
            //if(j-i==1&&s[j]==s[i])
            //dp[i][j]=1;
        }
        for(int i=0;i<n;i++)
            for(int j=i+1;j<n;j++)
            {
                if(s[i]==s[j])
                {
                    dp[i][j]=dp[i+1][j-1]+1;
                    if(dp[i][j]>maxn)
                    maxn=dp[i][j];
                }
                
                else
                dp[i][j]=0;
            }
            cout<<maxn<<endl;    
            return 0;
    }

    柳神代码

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #define inf 1001
    using namespace std;
    int main()
    {
        string s;
        int n,dp[inf][inf],ans=1;
        getline(cin,s);
        n=s.size();
        fill(dp[0],dp[0]+inf*inf,0);
        for(int i=0;i<n;i++)
        {
            dp[i][i]=1;
            if(s[i]==s[i+1]&&i<n-1)
            {
            dp[i][i+1]=1;
            ans=2;
        }
        }
        for(int l=2;l<=n;l++)
            for(int i=0;l+i-1<n;i++)
            {
                int j=l+i-1;
                if(s[i]==s[j]&&dp[i+1][j-1]==1)
                {
                    dp[i][j]=1;
                    ans=l;
                }
            }
            cout<<ans<<endl;    
            return 0;
    }

    dp还得好好学习啊

    思维不行

    如果你够坚强够勇敢,你就能驾驭他们
  • 相关阅读:
    vtk 矩阵管理系统
    在opengl中使用纹理
    [译文]:单元测试的七种境界
    [翻译]:六分钟八法则塑造优秀程序员
    weekly review 200921: Power Sleep
    Bye, Scofield
    weekly review 200922: Goal
    weekly review 200920: Prototype Demo
    转载:测试驱动开发三原则
    weekly review 200918: productive
  • 原文地址:https://www.cnblogs.com/liuzhaojun/p/11162217.html
Copyright © 2020-2023  润新知