#include "OJ.h" #include <iostream>using namespace std;/* function: Calculates the minimum period of a string. Prototype: int getminperiod (char *string); input parameter: char * string: String. return value: int string minimum period. Idea: Use I to denote a possible period of a string, starting from a period of 1 to gradually increase by 1, when I meet the period of the string is returned; I satisfy the condition that is the period of the string: 1, save the first I character, all the strings are compared with the string, no different characters 2, The length of all strings K is an integer multiple of I */int getminperiod (char *inputstring) {/ * here implements the function */if (inputstring = NULL | | *inputstring = = ')} { return 0;} for (int i =1; I <=100; ++i) {//char *p = Inputstring;char *temp = new Char [i];bool flag = false;for (int j =0; J < I ++J) {Temp[j] = inputstring[j];} int k = i; while (inputstring[k]! = ' + ') {if (temp[k%i]! = Inputstring[k]) {flag =true;break;} k++;} Delete [] temp;if (flag = = False && k%i = = 0) {return i;}} return 0;} int main () {char inputstring[13] = "ABCABCABCABC"; Cout<<getminperiod (inputstring) <<endl;return 0;}
Periodic string problems