其他分享
首页 > 其他分享> > PAT 1040 最长回文串

PAT 1040 最长回文串

作者:互联网

PAT 1040 最长回文串

dp,练手

代码

#include <bits/stdc++.h>

using namespace std;

string s;
const int maxn = 1005;

int dp[maxn][maxn];

int dpf(){
    int max = 1;
    for(int r = 2;r <= s.size();r++){
        for(int i = 0;i <= s.size()-r;i++){
            dp[i][i+r] = dp[i+1][i+r-1] * (s[i]==s[i+r-1]);
            if(dp[i][i+r]) max = r;
        }
    }
    return max;
}

int main(){
    char c;
    while(true){
        if((c = getchar()) =='\n') break;
        s += string(1,c);
    }

    fill(dp[0], dp[0]+maxn*maxn, 1);
    if(s==""){
        cout<<0<<endl;
        return 0;
    }
    cout<<dpf()<<endl;
}

12ms

标签:1040,PAT,string,int,max,maxn,dp,回文
来源: https://blog.csdn.net/qq_43513855/article/details/117371492