DP PKU 2138

把输入的字符串按照长度从小到大排序

Travel Games
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 3984 Accepted: 1426

Description

The cows are taking a trip to the lakes in Minnesota. Like everyone else, they are bored and are playing "travel games" to pass the time away. 

In this travel game, the first cow thinks of a three letter word from the Sacred Travel Game Dictionary (STGD). The next cow in line must add a letter to the word (at the beginning, between two letters, or at the end) to make another word in the STGD. The cows are curious as to just how big the final word can be. 

Given a dictionary of D (1 <= D <= 1000) words and a starting word, find any of the longest possible words that can be formed playing this travel game. 

Input

* Line 1: The integer D followed by a space followed by a legal three letter word. 

* Line 2 through D+1: Each line contains a legal word no longer than 80 characters, consisting only of lowercase letters, from the STGD. 

Output

A single line with the longest word that can be formed by extending the input seed. 
The input ensure the correct result will be unique.

Sample Input

9 cal
cal
calf
calfs
call
calls
choral
chorale
coal
coral

Sample Output

chorale

Hint

[From the sequence: cal, coal, coral, choral, chorale]

Source

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <limits.h>
#include <ctype.h>
#include <string.h>
#include <string>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <deque>
#include <vector>
#include <set>
#include <map>
using namespace std;
#define MAXN 100

struct node{
    int len;
    char s[MAXN];
}a[1111];
int dp[1111];
bool cmp(node a,node b){
    return a.len < b.len;
}

int main(){
    int n,i,j,k;
    char ss[MAXN];

    while(cin>>n){
        scanf("%s",ss);
        memset(dp,0,sizeof(dp));
        strcpy(a[0].s,ss);
        a[0].len = strlen(a[0].s);
        for(i=1;i<=n;i++){
            scanf("%s",a[i].s);
            a[i].len = strlen(a[i].s);
        }
        sort(a+1,a+1+n,cmp);
        dp[0] = 3;
        int ans;
        for(i=1;i<=n;i++){
            ans = 0;
            for(j=0;j<i;j++){
                if(a[j].len+1 != a[i].len){
                    continue;
                }
                int con = 0;
                for(k=0;k<a[i].len;k++){
                    if(a[i].s[k] == a[j].s[con]){
                        con++;//匹配是否成功
                    }
                }
                if(con == a[j].len){
                    ans = dp[j]+1;
                }
                dp[i] = max(ans,dp[i]);
            }
        }
        ans = 0;
        for(i=0;i<=n;i++){
            if(ans < dp[i]){
                ans = dp[i];
            }
        }
        for(i=0;i<=n;i++){
            if(dp[i] == ans){
                printf("%s\n",a[i].s);
            }
        }
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值