华为OJ字符串运用-密码截取

描述

Catcher是MCA国的情报员,他工作时发现敌国会用一些对称的密码进行通信,比如像这些ABBA,ABA,A,123321,但是他们有时会在开始或结束时加入一些无关的字符以防止别国破解。比如进行下列变化 ABBA->12ABBA,ABA->ABAKK,123321->51233214 。因为截获的串太长了,而且存在多种可能的情况(abaaab可看作是aba,或baaab的加密形式),Cathcer的工作量实在是太大了,他只能向电脑高手求助,你能帮Catcher找出最长的有效密码串吗?


知识点 字符串,循环,函数,指针,枚举,位运算,结构体,联合体,文件操作,递归
运行时间限制 10M
内存限制 128
输入

输入一个字符串

输出

返回有效密码串的最大长度

样例输入 ABBA
样例输出 4
#include <iostream>  
#include <string>  
#include <vector>
#include <algorithm>
//#include "Customer.h"

#define Max(a,b) (a)>(b)?(a):(b)
using namespace std;

int getCatcher(string str, int i)
{
	int count1 = 0, count2 = 0;
	int pre = i - 1;
	int post = i + 1;

	while (pre>=0 && post<str.length() && str[pre] == str[post] )
	{
		pre--;
		post++;
	}		
	count1 = post - pre - 1;
		
	
	pre = i;
	post = i + 1;
	while (pre >= 0 && post<str.length() && str[pre] == str[post] )
	{
		pre--;
		post++;
	}
	count2 =  post - pre - 1;
		
	return Max(count1, count2);
}
int main()
{
	string str;
	cin >> str;
	int max = 0;
	for (int i = 1; i < str.length(); i++)
	{
		max = Max(max, getCatcher(str, i));
	}
	cout << max;
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值