题解 | #密码截取#

密码截取

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

// HJ32 密码截取.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
#include<iostream>
#include<bits/stdc++.h>

using namespace std;

class Solution {
public:
	int Pwcut(string& s,int l,int r);
};

int Solution::Pwcut(string& s,int l,int r)
{
	while (l >= 0 && r <=s.size()-1)
	{
		if (s[l] == s[r])
		{
			l--;
			r++;
		}
		else
			break;
	}
	return r - l - 1;
}

int main()
{
	Solution a;
	string s;
	while (getline(cin, s))
	{
		if (s.size() == 0)
		{
			cout << "0" << endl;
			continue;
		}
		if (s.size() == 1)
		{
			cout << "1" << endl;
			continue;
		}
		int maxnums = 0;
		for (int i = 0; i < s.size(); i++)
		{
			int len1 = a.Pwcut(s, i, i);
			int len2 = a.Pwcut(s, i,i+1);
			len1 = max(len1, len2);
			maxnums = max(maxnums, len1);
		}
		cout << maxnums << endl;
	}
	return 0;
}

全部评论

相关推荐

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