Cod sursa(job #1093062)

Utilizator marian68Vlad Ghircus marian68 Data 27 ianuarie 2014 18:35:28
Problema Text Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.56 kb
#include<cstdio>
#include<cstring>
using namespace std;
int i,n,m,x,j,p,cate,max1,max2,bun,d[20001],e[20001];
char a[201],b[20001][21],c[21];
int main()
{
    freopen("text3.in","r",stdin);
    freopen("text3.out","w",stdout);
    gets(a);
    n=0;
    while(!feof(stdin))
    {
        m=-1;
        x=strlen(a);
        if(x==0) gets(a);
        else
        {
            for(i=0;i<=x;i++)
            {
                if(a[i]>='a'&&a[i]<='z')
                {
                    m++;
                    c[m]=a[i];
                }
                else if(a[i]==' '||i==x)
                {
                    m++;
                    c[m]='\0';
                    n++;
                    strcpy(b[n],c);
                    m=-1;
                }
            }
            gets(a);
        }
    }
    printf("%d\n",n);
    d[1]=1;
    max2=0;
    for(i=2;i<=n;i++)
    {
        bun=0;
        max1=0;
        for(j=i-1;j>=1;j--)
            if(b[i][0]==b[j][strlen(b[j])-1]&&d[j]>max1)max1=d[j];
        d[i]=max1+1;
        if(d[i]>=max2)
        {
            max2=d[i];
            p=i;
        }
    }
    printf("%d\n",n-max2);
    cate=1;
    e[1]=p;
    max2--;
    while(max2>0)
    {
        for(i=p-1;i>=1;i--)
            if(b[i][strlen(b[i])-1]==b[p][0]&&d[i]==max2)
            {
                p=i;
                cate++;
                e[cate]=p;
                max2--;
                break;
            }
    }
    for(i=cate;i>=1;i--)
        printf("%s\n",b[e[i]]);
    return 0;
}