顺丰笔试第一道编程题,求“双截棍字符串”
#include<iostream>
#include<string>
using namespace std;
int main(){
string str;
cin >> str;
int len1 = 1, len2 = 1, len3 = 1, len;
int begin = 0, max = 0, temp;
for (int i = 0; i<str.length() - 1; i){
len1 = 1, len2 = 1, len3 = 1;
if (str[i + 1] == str[i]){
temp = i;
while (str[i + 1] == str[i]){
len1++;
i++;
}
if (i == str.length()-1)
break;
else
i++;
while (str[i + 1] == str[i]){
len2++;
i++;
}
if (i == str.length()-1)
break;
else
i++;
while (str[i + 1] == str[i]){
len3++;
i++;
}
len = len1 + len2 + len3;
if (len1 == len3 && len1 > len2 && len > max){
begin = temp;
max = len;
i = temp + len1 + len2;
}
else
i = temp + len1;
}
else
i++;
}
if (max == 0)
cout << "NULL" << endl;
else{
cout << str.substr(begin, max) << endl;
}
system("pause");
return 0;
}