Cod sursa(job #30622)

Utilizator tm_raduToma Radu tm_radu Data 14 martie 2007 19:12:39
Problema Substr Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.26 kb
#include <stdio.h>
#include <string.h>

int n, m, k;
char *p[18001], t[18001];
int lung;
char *aux, c;
int i, j, imax;

void Quick(int st, int dr);
int Common(char *p1, char* p2);

int main()
{
    freopen("substr.in", "r", stdin);
    freopen("substr.out", "w", stdout);
    scanf("%d %d", &n, &k);
    scanf("%c", &c);
    for ( i = 0; i < n; i++ )
    {
        scanf("%c", &t[i]);
        p[i] = &t[i];
    }
    t[n] = '\0';
    
    Quick(0, n-1);
    for ( i = 0; i < n-k; i++ )
    {
        m = Common(p[i], p[i+k-1]);
        if ( m > lung )
            lung = m;
    }
    m = lung;
    printf("%d\n", lung);
    return 0;
}

void Quick(int st, int dr)
{
    int i = st-1;
    int j = dr+1;
    int mij = st;
    do
    {
        do { i++; } while ( strcmp(p[i], p[mij]) < 0 );
        do { j--; } while ( strcmp(p[mij], p[j]) < 0 );
        if ( i <= j )
        {
            aux = p[i];
            p[i] = p[j];
            p[j] = aux;
        }
    } while ( i <= j );
    if ( i < dr ) Quick(i, dr);
    if ( st < j ) Quick(st, j);
}

int Common(char* p1, char* p2)
{
    int L = 0;
    for ( ; *p1 && (*p1 == *p2); L++, p1++, p2++ );
    return L;
}