The King's Magic Mirror | Solutions

King's Mirror

Topic
Title Description

The king has a magic mirror that can double anything that touches the mirror surface-just, because it is a mirror, the added part is the opposite. For example, a necklace, we use AB to represent, different letters represent different colors of pearls. If you touch the B end to the mirror, the magic mirror will change this necklace to ABBA. If you touch with one end, it will become ABBAABBA (assuming that the king only touches the magic mirror with one end of the necklace).Given the final necklace, please write a program to output the smallest possible length of the original necklace before the king does not use the magic mirror.

Input format

There is only one string, composed of uppercase English letters (number of letters <= 100000), indicating the final necklace.

Output format

There is only one integer, which indicates the smallest possible length of the original necklace before the king used the magic mirror.


First
need to understand the meaning of the question


This question mainly depends on

Binary search & palindrome

This question requires a function

Write function in this figure is a palindrome if
the main function to write binary


The following is the process

Insert picture description here
If you want to master the palindrome,
you must be familiar with the subscript

perfect


code show as below

#include <string>
#include <iostream>
using namespace std;
bool ll(string a)
{
	int i,la;
	la=a.size();//取字符串长度 
	for(i=0;i<=la/2;i++)//判断回文 
	{
		if(a[i]!=a[la-1-i])
		{
			return 0; 
		}
	}
	return 1;
}
int main()
{
	int la;
    string a,i,b;
    cin>>a;
    la=a.size();
    while (la%2==0)//判断能否被2整除 否则输出后直接结束 
    {
    	if(ll(a))
    	{
    		a=a.substr(0,la/2);//折半 
    		la=la/2;
		}
		else    break;
	}
    cout<<la;
}

Like it or not

Just vomit if you like

Questions can be asked below to remember

@Y_bluefat

Published 5 original articles · won 10 · views 136

Guess you like

Origin blog.csdn.net/Y_bluefat/article/details/105603973