请问我的推理哪里有问题?
查看原帖
请问我的推理哪里有问题?
1152799
wsy_I楼主2024/6/26 20:34

首先,将字符串分成两半,将一段倒序与第二段比较,如相同,将字符串折半,如不相同,则为最小长度。(我加了一个特判,字符串长度为奇数时,返回该长度,因为继续往下没有意义。)

于是我得出代码如下:

#include<bits/stdc++.h>
using namespace std;
char s[100010],comp[100010],cnt;
void cut(int pos)
{
	for(int i=1;i<=strlen(comp);i++) comp[i]='\0';
	for(int i=strlen(s);i>=pos;i--){
		comp[i-pos]=s[i];
		s[i]='\0';
	}
}
bool judge()
{
	if(strlen(s)!=strlen(comp)) return false;
	for(int i=1;i<=strlen(s);i++){
		if(s[i]!=comp[strlen(comp)-i]) return false;
	}
	return true;
}
int main()
{
	cin>>s;
	if(strlen(s)%2==1){
		cout<<1;
		return 0;
	}
	while(strlen(s)>=1){
		cut(strlen(s)/2);
		if(!judge){
			cout<<strlen(s)*2;
			return 0;
		}
		else if(strlen(s)%2==1){
			cout<<strlen(s)*2;
			return 0;
		}
	}
	return 0;
}

到底哪里有问题?

2024/6/26 20:34
加载中...