uva10602 - Editor Nottoobad

来源:互联网 发布:网络到internet有个x 编辑:程序博客网 时间:2024/05/22 17:25

大水题。。。

比较前缀,,,,,

代码如下:

#include <cstdio>#include <cstring>#define M 105char 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;}