uva10602 - Editor Nottoobad

大水题。。。

比较前缀,,,,,

代码如下:

#include <cstdio>
#include <cstring>
#define M 105
char st[M][M];
int path[M], fl[M];
int get_result(int a, int b)
{
    int len = strlen(st[a]);
    for(int i = 0; i < len; i++)
    if(st[a][i]!=st[b][i]) return i;
    return len;
}
int main ()
{
    int t, n, ans, max;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i = 0; i < n; i++) scanf("%s",st[i]);
        ans = strlen(st[0]); path[0] = 0;
        memset(fl,0,sizeof(fl));
        for(int i = 0; i < n; i++)
        {
            max = -1;
            for(int j = 1; i<n&&j < n; j++)
            {
                if(fl[j]) continue;
                int tt = get_result(path[i], j);
                if(max<tt) {path[i+1] = j; max = tt; }
            }
            fl[path[i+1]] = 1;
            if(i<n-1) ans += strlen(st[path[i+1]]) - max;
        }
        printf("%d\n",ans);
        for(int i = 0; i < n; i++) printf("%s\n",st[path[i]]);
    }
    return 0;
}


posted on 2013-03-31 11:19  Primo...  阅读(141)  评论(0)    收藏  举报