HJ32 密码截取
密码截取
描述
Catcher是MCA国的情报员,他工作时发现敌国会用一些对称的密码进行通信,比如像这些ABBA,ABA,A,123321,但是他们有时会在开始或结束时加入一些无关的字符以防止别国破解。
比如进行下列变化ABBA->12ABBA,ABA->ABAKK,123321->51233214 。
因为截获的串太长了,而且存在多种可能的情况(abaaab可看作是aba,或baaab的加密形式),Cathcer的工作量实在是太大了,他只能向电脑高手求助,你能帮Catcher找出最长的有效密码串吗?
> 数据范围:字符串长度满足 1≤n≤2500
> 输入描述:输入一个字符串(字符串的长度不超过2500)
> 输出描述:返回有效密码串的最大长度
> 示例:
- 输入:12HHHHA
- 输出:4
using System;
public class Program
{
public static void Main()
{
string str;
str = Console.ReadLine();
int count = 0;
for (int k = 0; k < str.Length; k++)
{
int dex = str.LastIndexOf(str[k]);
if (dex == k) continue;
int q = 0;
string s = str;
if (dex < str.Length-1)
{
s = str.Remove(dex + 1);
}
for (int i = k, j = dex; j > i;)
{
char ds = str[i];
char df = str[j];
if (str[i] == 'o' && str[i+1]=='e' && str[i + 2] == 'f' && i==k)
{
string fg = str.Substring(i,j-i);
}
if (str[i] == str[j])
{
q += 2;
i++;
j--;
s = str.Remove(j);
if (i == j) q++;
}
else
{
q = 0;
i = k;
if (str[k] != str[j])
{
j = s.LastIndexOf(str[k]);
s = str.Remove(j + 1);
}
}
}
count = count < q ? q : count;
}
Console.WriteLine(count);
}
}