题解 | #密码截取#C++解法,就是求最长回文数,两个for循环暴力解

密码截取

http://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string str;
    while(cin>>str)
    {
        int len=0;//记录最长回文长度
        for(int i=0;i<str.size();i++)
        {
            for(int j=str.size()-1;j>i;j--)
            {
                int m,n;
                int tmp=0;
                m=i;n=j;
                while(str[m]==str[n])
                {
                    tmp++;
                    m++;n--;
                    if(m>=n)//由此判断回文成立
                    {
                        if(m>n) tmp=2*tmp;//abba型
                        else tmp=2*tmp+1;//aba型
                        if(len<tmp) len=tmp;
                        break;
                    }
                }
            }
        }
        cout<<len<<endl;
    }
}
全部评论

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务