The code for UVaOJ 455 isn't being accepted, what's wrong?

19 hours ago 2
ARTICLE AD BOX

I know the algorithm can be improved, but the issue isn't time, it just says "wrong answer".

I also doesn't quite understand the blank-line requirement of the problem.

A character string is said to have period k if it can be formed by concatenating one or more repetitions of another string of length k. For example, the string ”abcabcabcabc” has period 3, since it is formed by 4 repetitions of the string ”abc”. It also has periods 6 (two repetitions of ”abcabc”) and 12 (one repetition of ”abcabcabcabc”). Write a program to read a character string and determine its smallest period.

Input

The first line oif the input file will contain a single integer N indicating how many test case that your program will test followed by a blank line. Each test case will contain a single character string of up to 80 non-blank characters. Two consecutive input will separated by a blank line.

Output

An integer denoting the smallest period of the input string for each input. Two consecutive output are separated by a blank line.

#include <bits/stdc++.h> using ll = long long; using namespace std; string GivenString; int main() { int CaseAmount; cin >> CaseAmount; int TotalCaseCount; for(TotalCaseCount = 1; TotalCaseCount <= CaseAmount; TotalCaseCount++) { cin >> GivenString; int CharCycle; int TotalLenth = GivenString.length(); for(CharCycle = 0; CharCycle < TotalLenth; CharCycle++) { string FirstNChar = GivenString.substr(0, CharCycle + 1); //Now try to use loops to add FirstNChar to it self until the size matches GivenString. And then compare string ComparString; ComparString.clear(); int GivenStrSize = GivenString.size(); int FullDiv = GivenStrSize / (CharCycle + 1); int RepeatCycle; for(RepeatCycle = 1; RepeatCycle <= FullDiv; RepeatCycle++) { ComparString += FirstNChar; }//for loop of every amount of first few characters if(ComparString == GivenString) break;//compare the given and the crafted string }//for loop of every cycle if(TotalCaseCount == CaseAmount) cout << (CharCycle + 1); else cout << (CharCycle + 1) << endl<<endl; }//for loop of every case return 0; }

HWG's user avatar

New contributor

HWG is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

4

Read Entire Article