您的位置:首页 > 其它

【华为oj】密码截取(字符串对称)

2015-08-06 16:00 405 查看
描述
Catcher是MCA国的情报员,他工作时发现敌国会用一些对称的密码进行通信,比如像这些ABBA,ABA,A,123321,但是他们有时会在开始或结束时加入一些无关的字符以防止别国破解。比如进行下列变化 ABBA->12ABBA,ABA->ABAKK,123321->51233214 。因为截获的串太长了,而且存在多种可能的情况(abaaab可看作是aba,或baaab的加密形式),Cathcer的工作量实在是太大了,他只能向电脑高手求助,你能帮Catcher找出最长的有效密码串吗?
样例输入:ABBA12ABBAAABAKK51233214abaaab样例输出:441365可以使用中提供的库函数。实现接口,每个接口实现1个基本操作:voidGetCipherMaxLen(characCipherContent[],int *piCipherLen):acCipherContent是一个字符串数组常量,见参考用例;piCipherLen为输出有效密码串的最大长度;题目框架中有2个参考用例,其它用例请执行编写。

知识点字符串
运行时间限制10M
内存限制128
输入输入一串字符
输出输出有效长度
样例输入ABBA
样例输出4
import java.util.Scanner;

public class  Main
{
public static void main(String[] args)
{
Scanner cin = new Scanner(System.in);
String str = cin.nextLine();
int len=MaxLength(str);
System.out.println(len);
cin.close();
}

private static int MaxLength(String str) {
// TODO Auto-generated method stub
if(str==null||str.length()==0)
{
return -1;
}
int symLen=1;
char[] letter=str.toCharArray();
int strLen=str.length();
int curIndex=1;
while(curIndex>0&&curIndex<strLen-1)
{
//odd symmetrical length,the 'pivot' char is letter[curIndex]
int i=curIndex-1;
int j=curIndex+1;
while(i>=0&&j<=(strLen-1)&&letter[i]==letter[j])
{
i--;
j++;
}
int newLen=j-i-1;
if(newLen>symLen)
{
symLen=newLen;
}
//even symmetrical length,the 'pivot' chars are letter[curIndex] and letter[curIndex+1]
i=curIndex;
j=curIndex+1;
while(i>=0&&j<=(strLen-1)&&letter[i]==letter[j])
{
i--;
j++;
}
newLen=j-i-1;
if(newLen>symLen)
{
symLen=newLen;
}
curIndex++;
}
return symLen;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: