#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
bool mirror(char *str,int length)
{
if (length % 2 != 0)//字符串长度为偶数直接返回错误
{
return false;
}
for (int i = 0; i < length; i++)
{
if (str[i] != str[length - 1 - i])
{
return false;
}
}
return true;
}
int judge(char *str,int length)
{
if(!mirror(str, length))
{
return length;
}
else
{
judge(str, length / 2);
}
}
int main()
{
char str [100000];
scanf_s("%s",str,100000);
cout << judge(str, strlen(str))<< endl;
}
cout << judge(str, strlen(str))<< endl;
}
还行
标程:
//#include <bits/stdc++.h>
#include <bits.h>
#include <stdio.h>
#include <iostream>
//#include <stdc++.h>
using namespace std;
int main()
{
string st;
cin >> st;
bool f = true;
while (f == true && st.size() >= 1) {//&&后的条件很重要,最初项链的长度必须大于1
string t = st;//记录原项链
reverse(st.begin(), st.end());//reverse可以翻转元素顺序
if (t != st || st.size() % 2 == 1) f = false;//while循环出口
//复制过的项链长度必须是偶数
else {
st = st.substr(0, st.size() / 2);//截取一半
}
}
cout << st.size() << "\n";//cout中\n比end快
return 0;
}