跳到正文

个人记录

P2799 国王的魔镜

#SUSPENDED#约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年1月13日
篇幅
约 1 分钟

版本

  1. v1当前

错误原因

没有判断长度是否为偶数

改正方法

增加判断长度的语句

正确代码

C++
#include<bits/stdc++.h>
using namespace std;
string s;
bool check(string x){
	int l=0,r=x.size()-1;
	for(int i=1;i<=x.size()/2;i++){
		if(s[l]!=s[r]){
			return false;
		}
	}
	return true;
}
int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>s;
	int len=s.size();
	if(len%2==1){
		cout<<len;
		return 0;
	}
	while(len%2==0&&check(s)){
		len/=2;
		s=s.substr(0,len);
	}
	cout<<s.size();
	return 0;
}
原文