Cod sursă (job #94609)

Utilizator avatar cristinamatei Matei Cristina Gabriela cristinamatei IP ascuns
Problemă Bart (clasele 9-10) Compilator cpp | 0,64 kb
Rundă Tema 8 clasele 9-10 2014/15 Status evaluat
Dată 2 dec. 2014 23:10:14 Scor 80
#include <fstream>
#include<cstring>
#include<iostream>

using namespace std;

ifstream in("bart.in");
ofstream out("bart.out");

int a[500001], n;
char P[500001];

void preprocesare() {
    a[0] = 0;
    int k = 0;
    for (int q = 1; q < n; q++) {
      while ((k > 0) && (P[k] != P[q])) {
        k = a[k - 1];
      }
      if (P[k] == P[q]) {
        k++;
      }
      a[q] = k;
    }
  }

int main()
{
    int i, x;
    in >> P;
    n = strlen(P);
    preprocesare();
    for ( i = 0; i < n; i++ )
        if ( a[i] == 0 )
            x = i;
    for ( i = 0; i <= x; i++ )
        out << P[i];
    return 0;
}