Cod sursa(job #99038)

Utilizator nimicLeoveanu Mihaita Alexandru nimic Data 10 noiembrie 2007 20:28:30
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Happy Coding 2007 Marime 1.21 kb
#include <stdio.h>
#include <algorithm>
#include <cmath>
#define x first
#define y second

using namespace std;

char text[10<<20];
unsigned n, lung, L, a[50500];

unsigned lungime(char *s, unsigned &x){
         unsigned k=0;
         while ('a'<=s[k] && s[k]<='c')
               x = 3*x + (s[k++]-'a');
         return k;
}

inline int exista(unsigned k){
         unsigned poz=0;
         for (int p=15; p>=0; p--)
             if (poz+(1u<<p)<n && a[poz+(1<<p)]<=k) poz+=(1u<<p);
         return a[poz]==k;
}

int main()
{
    unsigned i;
    freopen("abc2.in", "r", stdin);
    freopen("abc2.out", "w", stdout);
    fgets(text, 10<<20, stdin);
    char lin[30];
    fgets(lin, 30, stdin);
    lung = lungime(lin, a[0]);
    while (lungime(lin, a[++n])==lung){
          if (fgets(lin, 30, stdin)==NULL){
             n++;
             break;
          }
    }
    n--;
    sort(a, a+n);
    unsigned nr,sol, pow=1;
    for (i=1; i<lung; i++)
        pow*=3;
    for (i=nr=0; i<lung; i++)
        nr=nr*3+(text[i]-'a');
    while ('a'<=text[L] && text[L]<='c') L++;
    for (i=lung, sol=exista(nr); i<L; i++){
        nr-=(text[i-lung]-'a')*pow;
        nr=3*nr+(text[i]-'a');
        sol += exista(nr);
    }
    printf("%d\n", sol);
}