The main idea of the topic is to give a string and find its "minimum positive period". For example, abcabcabcabc takes 3 as its period, but at the same time, 6 and 12 are also its periods, but the minimum period is 3, which needs to be found. It should be noted that a string such as abcdef does not appear to have a period. In fact, its period is its own length of 6. What's more, the format in the title requires "Two consecutive output are separated by a blank line."
The solution can be the modular method mentioned in other blogs, or the substring truncation method in the string class. If the substring truncation with a certain length is equal to the end of the original string, the length is one of its cycles. How to find the minimum period? It's good to start with a length of 1. The code is as follows:
#include <iostream> #include <string> #include <cstdio> #include <cstring> using namespace std ; int main(){ int t ; cin >> t ; while ( t -- ){ string str ; cin >> str ; int len = 1 ; ///Intercept substring length variable bool flag = true ; ///Judge if the period is the string length while ( len <= str.size() / 2 ){ ///Because the period can be divided by the length of the string, and the period longer than half of the length cannot be divided by the length of the string, only half of the string length needs to be intercepted bool check = true ; ///The case that the judgment period is not the string length string str2 = str.substr(0 , len) ; // cout << "str2 = " << str2 << endl ; for ( int i = len ; i < str.size() ; i += len ){ string str3 = str.substr(i , len) ; // cout << "str3 = " << str3 << endl ; if ( str2 != str3 ){ check = false ; break ; } } if ( len == str.size() / 2 && check == false ){ ///If len is half of the string length at this time and check is not successful before that, modify the flag variable at this time flag = check ; } if ( check ){ break ; } // cout << str2 << endl ; len ++ ; } if ( !flag ) cout << str.size() << endl ; else cout << len << endl ; if ( t ) ///Pay attention to format control putchar('\n') ; } return 0 ; }