Letter Game_usaco4.3_枚举

55 篇文章 0 订阅
53 篇文章 0 订阅

这里写图片描述

Description


Figure 1: Each of the 26 lowercase letters and its value
Letter games are popular at home and on television. In one version of the game, every letter has a value, and you collect letters to form one or more words giving the highest possible score. Unless you have `a way with words’, you will try all the words you know, sometimes looking up the spelling, and then compute the scores. Obviously, this can be done more accurately by computer.

Given the values in Figure 1, a list of words, and the letters collected: find the highest scoring words or pairs of words that can be formed.

PROGRAM NAME: lgame

INPUT FORMAT


One line with a string of lowercase letters (from a' toz’). The string consists of at least 3 and at most 7 letters in arbitrary order.

DICTIONARY FORMAT


At most 40,000 lines, each containing a string of at least 3 and at most 7 lowercase letters. At the end of this file is a line with a single period (`.’). The file is sorted alphabetically and contains no duplicates.
.

OUTPUT FORMAT


On the first line, your program should write the highest possible score, and on each of the following lines, all the words and/or word pairs from file lgame.dict with this score. Sort the output alphabetically by first word, and if tied, by second word. A letter must not occur more often in an output line than in the input line. Use the letter values given in Figure 1.

When a combination of two words can be formed with the given letters, the words should be printed on the same line separated by a space. The two words should be in alphabetical order; for example, do not write rag prom', only writeprom rag’. A pair in an output line may consist of two identical words.

Analysis


表示不会用c++的字符数组,蛋疼
先排除字典中不含给出单词字母的,然后 O(n2) 暴力枚举咯,最后找答案的时候记得要按字典序输出

Code


/*
ID:wjp13241
PROG:lgame
LANG:C++
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define N 40001
#define W 27
#define L 8
using namespace std;
char t[N][L],wd[L],d[N][L],prt[N*10+1][L+2];
int v[W],w[W],p[N],score[]={2,5,4,4,1,6,5,5,1,7,6,3,5,2,3,5,7,2,1,2,4,6,6,7,5,7};
int max(int x,int y){return x>y?x:y;}
int main()
{
    freopen("lgame.in","r",stdin);
    freopen("lgame.out","w",stdout);
    scanf("%s",wd);
    fclose(stdin);
    freopen("lgame.dict","r",stdin);
    for (int i=0;i<strlen(wd);i++)
        w[wd[i]-'a']++;
    char s[L];
    int cnt=0;
    int ans=0;
    while (scanf("%s",s)&&s[0]!='.')
    {
        if (strlen(s)>7||strlen(s)<3)
            continue;
        for (int i=0;i<W;i++)v[i]=w[i];
        int val=0;
        bool flag=false;
        for (int i=0;i<strlen(s);i++)
            if (!v[s[i]-'a'])
            {
                flag=true;
                break;
            }
            else
                v[s[i]-'a']--,val+=score[s[i]-'a'];
        if (!flag)
        {
            p[++cnt]=val;
            strcpy(d[cnt],s);
            ans=max(ans,p[cnt]);
        }
    }
    for (int i=1;i<=cnt;i++)
        for (int j=i+1;j<=cnt;j++)
            if (strlen(d[i])+strlen(d[j])<8)
            {
                char s[L]="";
                for (int k=0;k<strlen(d[i]);k++)
                    s[k]=d[i][k];
                for (int k=0;k<strlen(d[j]);k++)
                    s[k+strlen(d[i])]=d[j][k];
                for (int k=0;k<W;k++)v[k]=w[k];
                int val=0;
                bool flag=false;
                for (int k=0;k<strlen(s);k++)
                    if (!v[s[k]-'a'])
                    {
                        flag=true;
                        break;
                    }
                    else
                        v[s[k]-'a']--,val+=score[s[k]-'a'];
                if (!flag)
                    ans=max(ans,p[i]+p[j]);
            }
    printf("%d\n",ans);
    int ansCnt=0;
    for (int i=1;i<=cnt;i++)
        if (p[i]==ans)
            strcpy(prt[++ansCnt],d[i]);
    for (int i=1;i<=cnt;i++)
        for (int j=i+1;j<=cnt;j++)
            if (strlen(d[i])+strlen(d[j])<8)
            {
                char s[L]="";
                for (int k=0;k<strlen(d[i]);k++)
                    s[k]=d[i][k];
                for (int k=0;k<strlen(d[j]);k++)
                    s[k+strlen(d[i])]=d[j][k];
                for (int k=0;k<W;k++)v[k]=w[k];
                int val=0;
                bool flag=false;
                for (int k=0;k<strlen(s);k++)
                    if (!v[s[k]-'a'])
                    {
                        flag=true;
                        break;
                    }
                    else
                        v[s[k]-'a']--,val+=score[s[k]-'a'];
                if (!flag)
                    if (ans==p[i]+p[j])
                    {
                        ++ansCnt;
                        for (int k=0;k<strlen(d[i]);k++)
                            prt[ansCnt][k]=d[i][k];
                        prt[ansCnt][strlen(d[i])]=' ';
                        for (int k=0;k<strlen(d[j]);k++)
                            prt[ansCnt][1+k+strlen(d[i])]=d[j][k];
                    }
            }
    for (int i=1;i<=ansCnt;i++)
        for (int j=i+1;j<=ansCnt;j++)
            if (strcmp(prt[i],prt[j])==1)
            {
                char tmp[L+2]="";
                strcpy(tmp,prt[i]);
                strcpy(prt[i],prt[j]);
                strcpy(prt[j],tmp);
            }
    // sort(prt+1,prt+ansCnt+1);
    for (int i=1;i<=ansCnt;i++)
        printf("%s\n",prt[i]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值