最大文字数
6890 ワード
最大文字数の計算
文字列を与えて、文字列の最大回文長を判断します.
Input Specification:
文字列を入力します.
Output Specification:
最大リターンの長さを表す数字を出力します.
Sample Input 1:
文字列を与えて、文字列の最大回文長を判断します.
Input Specification:
文字列を入力します.
Output Specification:
最大リターンの長さを表す数字を出力します.
Sample Input 1:
ABCBA
Sample Output 1:5
Sample Input 2:ABCBD
Sample Output 2:3
コード:#include
#include
using namespace std;
int main()
{
string str, tmp;
while(cin >> tmp) {
// , “#”
str = "#";
for(int i = 0; i < tmp.size(); ++i) {
str = str + tmp[i] + "#";
}
// cout << str << endl;
int maxlength = 1, tmplength;
//str[0] , 1 , str[1]
for(int i = 1; i < str.size(); ++i) {
//
// , ,
if(i + maxlength >= str.size()) break;
// , , i , maxlength-1, ,
if(str[i-maxlength] != str[i+maxlength]) continue;
else {
// , i , ,
tmplength = 1;
//
while(i+tmplength < str.size() && str[i-tmplength] == str[i+tmplength])
tmplength++;
// , , tmplength -1
tmplength--;
}
maxlength = max(maxlength, tmplength);
}
cout << maxlength << endl;
}
return 0;
}