您的位置:首页 > 其它

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

2017-04-12 17:29 204 查看
描述
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: