UVA 10602 Editor Nottoobad

 

大意:一台机器可以使用2种命令,即复制最后一个单词,将最后一个单词的最后一个字母删去,问你使用最少打印的字母打印所有的字符串。

 

思路:每次枚举出于前一个字符串具有最长长度的公共前缀,贪心性质可以被证明,直到所有的字符串都被包括。

 

CODE:

 

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using  namespace std;

#define MAXN 1001

char str[MAXN][MAXN];
int save[MAXN], n;

void solve()
{
     bool vis[MAXN] = { 0};
     int tmp;
     int cur =  0, tot =  1, ans = strlen(str[ 0]);
    save[ 0] =  0;
     for( int i =  1; i < n; i++)
    {
         int max = - 1;
        vis[cur] =  1;
         for( int j =  0; j < n; j++)  if(!vis[j])
        {
             int k;
             for(k =  0; k < strlen(str[cur]) && k < strlen(str[j]); k++)
            {
                 if(str[cur][k] != str[j][k])  break;
            }
             if(max < k)
            {
                max = k;
                tmp = j;
            }
        }
        save[tot++] = cur = tmp;
        ans += strlen(str[cur]) - max;
    }
    printf( " %d\n ", ans);
     for( int i =  0; i < tot; i++)
    {
        printf( " %s\n ", str[save[i]]);
    }
}


int main()
{
     int T;
    scanf( " %d%*c ", &T);
     while(T--)
    {
        scanf( " %d ", &n);
         for( int i =  0; i < n; i++)
            scanf( " %s ", str[i]);
        solve();
    }
     return  0;
}

 

转载于:https://www.cnblogs.com/g0feng/archive/2012/10/18/2730012.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值