POJ 2004解题报告

    技术2022-05-20  44

    Mix and Build Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 3246 Accepted: 963Case Time Limit: 2000MS Special Judge

    Description

    In this problem, you are given a list of words (sequence of lower case letters). From this list, find the longest chain of words w1, ..., wn such that wi is a mixed extension of w i-1. A word A is a mixed extension of another word B if A can be formed by adding one letter to B and permuting the result. For example, "ab", "bar", "crab", "cobra", and "carbon" form a chain of length 5.

    Input

    The input contains at least two, but no more than 10000 lines. Each line contains a word. The length of each word is at least 1 and no more than 20. All words in the input are distinct.

    Output

    Write the longest chain that can be constructed from the given words. Output each word in the chain on a separate line, starting from the first one. If there are multiple longest chains, any longest chain is acceptable.

    Sample Input

    ab arc arco bar bran carbon carbons cobra crab crayon narc

    Sample Output

    ab bar crab cobra carbon carbons 题意:输出若干字符串中按照如下规则的最长的字符串序列(规则:从短到长,并且相邻的两个字符串只一个字符)。 思路:类似dp的方法,dp[i]=max(dp[i],dp[j]+1),其中j是i加一个字符变成的串...,按照这个思路写的代码 4875MS 险过。。  #include<iostream> #include<algorithm> using namespace std; char str[10010][30]; int lenth[10010],list[10010][26],next[10010],start,maxl,listl[10010],n; bool cmp(int i,int j) { int l=-1; i=lenth[i]0000; j=lenth[j]0000; for(int k=0;k<26;k++) if(list[i][k]!=list[j][k]&&l!=-1) return false; else if(list[i][k]!=list[j][k]) l=k; if(list[j][l]-list[i][l]==1) return true; } void find(int i) { int j=i+1; for(;j<n;j++) if(lenth[j]/100000-lenth[i]/100000==1&&cmp(i,j)&&listl[i]<listl[j]+1) { listl[i]=listl[j]+1; next[i]=j; } if(listl[i]>maxl) { maxl=listl[i]; start=i; } } int main() { int i; memset(list,0,sizeof(int)*10010*26); memset(next,0,sizeof(int)*10010); memset(listl,0,sizeof(int)*10010); n=0; while(scanf("%s",str[n])!=EOF) { for(i=0;str[n][i]!='/0';i++) list[n][str[n][i]-97]++; lenth[n]=i*100000+n; n++; } sort(lenth,lenth+n); maxl=0; for(i=n-1;i>-1;i--) find(i); do { cout<<str[lenth[start]0000]<<endl; start=next[start]; }while(start); }

    最新回复(0)