• 最长回文子串(练习...)


    代码:

    //最长回文串
    #include<iostream>
    #include<string>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int main() {
        int t;scanf("%d",&t);
        while(t--) {
            string s;cin>>s;
            string str=s;reverse(s.begin(),s.end());
            int len=s.length(),dp[len+1][len+1],longlen=0,r;
            memset(dp,0,sizeof(dp));
            for(int i=0;i<=len;i++) {
                for(int j=0;j<=len;j++) {
                    if(i==0||j==0) {
                        dp[i][j]=0;
                    } else if(str[i-1]==s[j-1]) {
                        dp[i][j]=dp[i-1][j-1]+1;
                        if(dp[i][j]>longlen) {
                            longlen=dp[i][j];r=i-1;
                        }
                    } else {
                        dp[i][j]=0;
                    }
                }
            }
            str=str.substr(r-longlen+1,longlen);
            cout<<str<<endl;
        }
        return 0;
    }
    


  • 相关阅读:
    vue
    手写Promise
    Promise应用
    Promise
    JS_URL模块
    模板字符串应用
    JS-OOP
    jQuery——过时,但是经典,关注核心点即可。
    MySql补充
    offset系列
  • 原文地址:https://www.cnblogs.com/lemonbiscuit/p/7776003.html
Copyright © 2020-2023  润新知