UVa : 10298 (Power Strings)



// http://uva.onlinejudge.org/external/102/10298.html
// Runtime: 0.808s
// Tag: Adhoc 

// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>

#define INF_MAX 2147483647
#define INF_MIN -2147483647
#define pi acos(-1.0)
#define N 1000005
#define LL long long

#define For(i, a, b) for( int i = (a); i < (b); i++ )
#define Fors(i, sz) for( size_t i = 0; i < sz.size (); i++ )
#define Fore(it, x) for(typeof (x.begin()) it = x.begin(); it != x.end (); it++)
#define Set(a, s) memset(a, s, sizeof (a))
#define Read(r) freopen(r, "r", stdin)
#define Write(w) freopen(w, "w", stdout)

int dr [] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dc [] = {0, 1, 1, 1, 0, -1, -1, -1};

using namespace std;

char ch [N + 10];

int main ()
{
    while ( scanf ("%s", ch) ) {
        if ( strcmp (ch, ".") == 0 ) break;

        int len = strlen (ch);
        int power = 1;

        for ( int i = 2; i <= len; i++ ) {
            if ( len % i == 0 ) {
                int L = len / i;
                bool flag = true;
                for ( int j = L; j < len; j++ ) {
                    if ( ch [j] != ch [j % L] ) { flag = false; break; }
                }

                if ( flag ) { power = max (power, i); }
            }
        }

        printf ("%d\n", power);
    }

	return 0;
}

// @END_OF_SOURCE_CODE

2 thoughts on “UVa : 10298 (Power Strings)

  1. @john
    so far i can remember,
    we need to find the shortest string that will repeat the rest of the string for 0 or more times

    @53
    i started the loop by guessing that our desire string is length 2
    if we found that this is our desired string then the loop will break
    otherwise, guess the desire string is length 3 and start over the process

    @54
    the length of the string has to be evenly divisible by our desired string length

    suppose, the string length is 12
    currently we are guessing our desired string length is 2
    so now we will divide our main string into 6 (12/2) parts
    if all parts are equal then our desired string length is 2

    otherwise, go to next step and guess our desired string length is 3
    so now we will divide our main string into 4 (12/3) parts
    if all parts are equal then our desired string length is 3

    and so on
    hope it will help 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s