Cod sursă (job #142525)

Utilizator avatar stefdascalescu Stefan Dascalescu stefdascalescu IP ascuns
Problemă Bart (clasele 9-10) Compilator cpp | 0,57 kb
Rundă Arhiva de probleme Status evaluat
Dată 13 apr. 2015 13:53:56 Scor 100
#include<cstdio>
#include<cstring>
using namespace std;
const int NMAX = 500000;
char str[NMAX + 5];
int pi[NMAX + 5];

int main() {
    freopen("bart.in", "r", stdin);
    freopen("bart.out", "w", stdout);
    int i, p, j, len;

    scanf("%s", str + 1);
    len = strlen(str + 1);

    p = 0;
    for(i = 2; i <= len; ++ i) {
        while(p && str[i]!=str[p+1])
            p=pi[p];
        if(str[i]==str[p+1])
            ++p;
        pi[i]=p;
    }
    for(j=1;j<=len-pi[len];++j)
        printf("%c", str[j]);
    printf("\n");
    return 0;
}