首页 > ACM题库 > HDU-杭电 > hdu 2397 Unique Words-动态规划-[解题报告]C++
2014
01-05

hdu 2397 Unique Words-动态规划-[解题报告]C++

Unique Words

问题描述 :

A common problem faced by electronic information providers is determining the number of unique words in a document. The case of a word does not affect its uniqueness. For example, The, tHE and The are all considered equivalent. Punctuation can appear in these documents and is handled as follows:
1) Periods ‘.’ and exclamation marks ‘!’ may appear at the end of a sentence and should not be considered a word, or part of a word.
2) Dashes ‘-’ appear between hyphenated words. The hyphenated words should be considered separately.
3) Commas ‘,’ colons ‘:’ and semicolons ‘;’ appear within a sentence and should not be considered a word, or part of a word.
4) Apostrophes ‘ appear within contractions and possessive forms. These symbols should be treated as if they never appeared (i.e., as if they were deleted from the word).

输入:

The input file contains a series of documents, each separated by an entire line of text containing only the word EOD Each document will contain no more than 1,000 lines and at most 100 unique words. All input lines will not contain more than 80 characters. Numbers, control characters, and punctuation symbols not listed above will not appear in the text. An entire line containing only the string EOT identifies the end of the list of documents; note this last document is terminated by EOT and not EOD

输出:

The input file contains a series of documents, each separated by an entire line of text containing only the word EOD Each document will contain no more than 1,000 lines and at most 100 unique words. All input lines will not contain more than 80 characters. Numbers, control characters, and punctuation symbols not listed above will not appear in the text. An entire line containing only the string EOT identifies the end of the list of documents; note this last document is terminated by EOT and not EOD

样例输入:

The banker hammered home his two-part message! His message, 
at times satirical, was that the bank's situation was a mess.
EOD
Hello world
EOD
This is a
final example
EOT

样例输出:

WORDS IN DOCUMENT #1
A
AT
BANKER
BANKS
HAMMERED
HIS
HOME
MESS
MESSAGE
PART
SATIRICAL
SITATUATION
THAT
THE
TIMES
TWO
WAS
WORDS IN DOCUMENT #2
HELLO
WORLD
WORDS IN DOCUMENT #3
A
EXAMPLE
FINAL
IS
THIS

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2915

                    http://acm.hdu.edu.cn/showproblem.php?pid=2379

题目的意思就是给定一些单词,可以用这些单词的任意组合作为密码。求一个密码长度为l,恰好由k个给定的单词组成的种数。

是个动态规划的题目,题目很好理解,用dp[i][j]表示由i个单词组成长度为j的密码的种数,则dp[i+1][j+k]=dp[i][j]*cnt[k](1<k<10)则本题就比较简单了。

因为单词个数最大为5即L,密码长度最大为50即LEN,单词长度最大为10即K,所以时间复杂度为O(L*LEN*K)。cnt【k】表示长度为k的单词总共由多少个。

代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>

using namespace std;

const int LEN=52;
const int N=7;

__int64 dp[N][LEN];
int cnt[11];

int main()
{
    int t,m,n,q,i,k,j,l;
    scanf("%d",&t);
    string a;
    while (t--)
    {
        scanf("%d%d%d",&m,&n,&q);
        while (m--)
        {
            cin>>a;
            cnt[a.length()]++;
        }
        memset(dp,0,sizeof(dp));
        for (i=1;i<=10;i++)
        {
            dp[1][i]=cnt[i];
            dp[i][i]=cnt[1];
        }
		for (i=0;i<5;i++)
        {
            for (k=0;k<=10;k++)
            {
                for (j=0;j<=LEN-k;j++)
                {
                    dp[i+1][j+k]+=dp[i][j]*cnt[k];
                }
            }
        }
        while (q--)
        {
            scanf("%d",&l);
            printf("%I64d\n",dp[n][l]);
        }
    }
    return 0;
}

解题转自:http://blog.csdn.net/iaccepted/article/details/6719507


  1. 一开始就规定不相邻节点颜色相同,可能得不到最优解。我想个类似的算法,也不确定是否总能得到最优解:先着一个点,随机挑一个相邻点,着第二色,继续随机选一个点,但必须至少有一个边和已着点相邻,着上不同色,当然尽量不增加新色,直到完成。我还找不到反例验证他的错误。。希望LZ也帮想想, 有想法欢迎来邮件。谢谢

  2. 可以参考算法导论中的时间戳。就是结束访问时间,最后结束的顶点肯定是入度为0的顶点,因为DFS要回溯

  3. for(int i=1; i<=m; i++){
    for(int j=1; j<=n; j++){
    dp = dp [j-1] + 1;
    if(s1.charAt(i-1) == s3.charAt(i+j-1))
    dp = dp[i-1] + 1;
    if(s2.charAt(j-1) == s3.charAt(i+j-1))
    dp = Math.max(dp [j - 1] + 1, dp );
    }
    }
    这里的代码似乎有点问题? dp(i)(j) = dp(i)(j-1) + 1;这个例子System.out.println(ils.isInterleave("aa","dbbca", "aadbbcb"));返回的应该是false

  4. 第一句可以忽略不计了吧。从第二句开始分析,说明这个花色下的所有牌都会在其它里面出现,那么还剩下♠️和♦️。第三句,可以排除2和7,因为在两种花色里有。现在是第四句,因为♠️还剩下多个,只有是♦️B才能知道答案。