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

来源:互联网 发布:scala和java混合使用 编辑:程序博客网 时间:2024/06/05 02:18

描述

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;}


0 0
原创粉丝点击