题解
【基础】国王的魔镜
1 条题解
-
0
解题思路
比较字符串两端的字符;如果两端相同,就继续检查里面的部分。
参考代码
// 这道题的做法:先读入题目给出的数据,再用简单的循环和判断完成要求。 #include<iostream> #include<cstring> using namespace std; bool mojing(string s,int end)//end下标 { bool f; if((end+1)%2==0){ f=true; for(int i=0;i<=end/2;i++){ if(s[i]!=s[end-i]){ f=false; break; } } } else{ f=false; } return f; } int main(){ string s; cin>>s; int end=s.size()-1; while(mojing(s,end)==true){//满足镜面要求一直循环 end=end/2; } cout<<end+1; return 0; }复杂度
代码只使用了简单变量、循环和判断。若循环检查了 n 个数据,时间复杂度通常为 O(n);没有开辟与输入规模相关的额外数组时,空间复杂度为 O(1)。
- 1