Monday, 13 August 2012

Period (Spoj 263, UVa 1328, UVaLive 3026, Regionals 2004 >> Europe - Southeastern)


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / uva ), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 1000010

string pattern;
int failur[MX],length_b,cnt;

void FailureFunction()
{
    int i=1,j=0;
    failur[0]=0;
    while(i<length_b)
    {
        if(pattern[i]==pattern[j])
        {
            j++;
            failur[i]=j;
            i++;
        }
        else if(j>0)
            j=failur[j-1];
        else
        {
            failur[i]=0;
            i++;
        }
    }
return;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t,kk=1;
    while(cin>>length_b && length_b)
    {
        cin>>pattern;
        CLR(failur);
        FailureFunction();
        cout<<"Test case #"<<kk++<<endl;

        for(int i=1; i<length_b; i++)
        {
            int miss_matched=i+1-failur[i];
            if((i+1)%miss_matched==0)
                if((i+1)/miss_matched>1)
                    cout<<i+1<<" "<<(i+1)/miss_matched<<endl;
        }
        cout<<endl;
    }
    return 0;
}

No comments:

Post a Comment