usaco Letter Game

先计算出每个单词的值,如果单词不合法则为-1,同时找出最大的单词。

然后输出。

/*
ID: modengd1
PROG: lgame
LANG: C++
*/
#include <iostream>
#include <stdio.h>
#include <memory.h>
#include <string>
#include <cstring>
#include <vector>
using namespace std;
int value[257];
int f[256];
bool useful[256];
bool legal[40000];
int valueofworde[40000];
int cal(string S,string S2)//次单词是否合法,不合法返回-1,合法返回这个词的值
{
    int counter[256];
    memset(counter,0,sizeof(counter));
    int ret=0;
    for(int i=0;i<S.size();i++)
    {
        counter[S[i]]++;
        if(!useful[S[i]]||counter[S[i]]>f[S[i]])
            return -1;
        ret+=value[S[i]];
    }
    for(int i=0;i<S2.size();i++)
    {
        counter[S2[i]]++;
        if(!useful[S2[i]]||counter[S2[i]]>f[S2[i]])
            return -1;
        ret+=value[S2[i]];
    }
    return ret;
}
void init()
{
    value[‘q‘]=value[‘j‘]=value[‘x‘]=value[‘z‘]=7;
    value[‘w‘]=value[‘f‘]=value[‘k‘]=value[‘v‘]=6;
    value[‘e‘]=value[‘s‘]=value[‘i‘]=1;
    value[‘r‘]=value[‘t‘]=value[‘a‘]=value[‘n‘]=2;
    value[‘o‘]=value[‘l‘]=3;
    value[‘u‘]=value[‘d‘]=value[‘c‘]=4;
    value[‘p‘]=value[‘g‘]=value[‘b‘]=value[‘m‘]=value[‘y‘]=value[‘h‘]=5;
}
int main()
{
    freopen("lgame.in","r",stdin);
    freopen("lgame.out","w",stdout);
    init();
    vector<string> dict;
    char ch;
    int maxvalue=0;
    char temp[10];
    FILE *in2 = fopen ("lgame.dict", "r");
    vector<int> index[43];//单词值为i的单词在字典中的位置
    memset(useful,false,sizeof(useful));
    memset(f,0,sizeof(f));
    while(~scanf("%c",&ch))
    {
        useful[ch]=true;
        f[ch]++;
    }
    for(int i =0;~fscanf(in2,"%s",temp);i++)
    {
        if(temp[0]==‘.‘)
            break;
        dict.push_back(temp);
        valueofworde[i]=cal(temp,"");
        if(valueofworde[i]!=-1)
            index[valueofworde[i]].push_back(i);
        maxvalue=max(maxvalue,valueofworde[i]);
    }
    for(int i=1;i<=42;i++)
    {
        for(int j=0;j<index[i].size();j++)
        {
            for(int k=0;k<=42;k++)
            {
                for(int m=0;m<index[k].size();m++)
                {
                    maxvalue=max(maxvalue,cal(dict[index[i][j]],dict[index[k][m]]));
                }
            }
        }
    }
    cout<<maxvalue<<endl;
    for(int i=0;i<dict.size();i++)
    {
        if(valueofworde[i]==maxvalue)
            cout<<dict[i]<<endl;
        else
        {
            int other=maxvalue-valueofworde[i];
            for(int j=0;j<index[other].size();j++)
            {
                if(index[other][j]>=i&&cal(dict[index[other][j]],dict[i])==maxvalue)
                {
                    cout<<dict[i]<<‘ ‘<<dict[index[other][j]]<<endl;
                }
            }
        }
    }
    return 0;
}

  

时间: 2024-08-08 13:48:19

usaco Letter Game的相关文章

USACO 4.3 Letter Game (字典树)

Letter GameIOI 1995 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 highes

[USACO 2011 Dec Gold] Threatening Letter【后缀】

Problem 3: Threatening Letter [J. Kuipers, 2002] FJ has had a terrible fight with his neighbor and wants to send him a nasty letter, but wants to remain anonymous. As so many before him have done, he plans to cut out printed letters and paste them on

USACO·2012·Feb Bronze &amp;&amp; 2009·Open Gold

Rope Folding [Brian Dean, 2012] 时间限制: 1 Sec 内存限制: 128 MB 题目描述 Farmer John has a long rope of length L (1 <= L <= 10,000) that he uses for various tasks around his farm. The rope has N knots tied into it at various distinct locations (1 <= N <=

COGS 696. [IOI1996][USACO 2.3] 最长前缀

★   输入文件:prefix.in   输出文件:prefix.out   简单对比时间限制:1 s   内存限制:128 MB 描述 USACO 2.3.1 IOI96 在生物学中,一些生物的结构是用包含其要素的大写字母序列来表示的.生物学家对于把长的序列分解成较短的序列(即元素)很感兴趣. 如果一个集合 P 中的元素可以通过串联(元素可以重复使用,相当于 Pascal 中的 “+” 运算符)组成一个序列 S ,那么我们认为序列 S 可以分解为 P 中的元素.元素不一定要全部出现(如下例中B

17. Letter Combinations of a Phone Number

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae", &q

USACO prefix TrieTree + DP

/* ID:kevin_s1 PROG:prefix LANG:C++ */ #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <vector> #include <map> #include <set> #include <algorithm> #include <cstdlib>

【USACO 1.3.4】牛式

[題目描述 ] 下面是一个乘法竖式,如果用我们给定的那n个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式. * * * x * * ---------- * * * * * * ---------- * * * * 数字只能取代*,当然第一位不能为0,况且给定的数字里不包括0. 注意一下在美国的学校中教的"部分乘积",第一部分乘积是第二个数的个位和第一个数的积,第二部分乘积是第二个数的十位和第一个数的乘积. 写一个程序找出所有的牛式. [格式] INPUT FORMAT: (f

USACO Chapter 1 Section 1.1

USACO的题解和翻译已经很多了... 我只是把自己刷的代码保存一下. 1.PROB Your Ride Is Here 1 /* 2 ID:xiekeyi1 3 PROG:ride 4 LANG:C++ 5 */ 6 7 #include<bits/stdc++.h> 8 using namespace std ; 9 10 int main() 11 { 12 freopen("ride.in","r",stdin); 13 freopen(&quo

USACO Your Ride Is Here

[USACO]Your Ride Is Here It is a well-known fact that behind every good comet is a UFO. These UFOs often come to collect loyal supporters from here on Earth. Unfortunately, they only have room to pick up one group of followers on each trip. They do,