当前位置: 首页 > 工具软件 > Lgame > 使用案例 >

usaco-lgame

姚淳
2023-12-01

首先过滤字典,把不符合要求的字典单词都pass掉,然后对字典进行两两组合再进行判断,就OK 了,有些细节要把握好。

觉得有点丑的代码

#include <iostream>
#include <fstream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#define zout fout
#define maxn 40000
using namespace std;
ifstream fin("lgame.in");
ifstream dic_fin("lgame.dict");
ofstream fout("lgame.out");
int icount_ans;
char dic[maxn][10];
int idic;
char ds[30];
int r[130],tr[130];
int len;
int score[130];
struct ansStruct
{
    char s1[10],s2[10];
    int sco;
    int len1,len2;
    ansStruct()
    {
        sco=0;
        s1[0]=s2[0]='\0';
    }
    void ss()
    {
        if(len2)
        if(strcmp(s1,s2)>0)
        {
            char temp[10];
            strcpy(temp,s1);
            strcpy(s1,s2);
            strcpy(s2,temp);
        }
    }
    void cal_score()
    {
        sco=0;
        len1=strlen(s1);
        len2=strlen(s2);
        for(int i=0; i<len1; i++)
        sco+=score[s1[i]];
        for(int i=0; i<len2; i++)
        sco+=score[s2[i]];
    }
    void print()
    {
        zout<<s1;
        if(len2)
        {
            zout<<" ";
            zout<<s2;
        }
        zout<<endl;
    }
    bool equ(ansStruct th)
    {
        if(strcmp(th.s1,s1))return false;
        if(strcmp(th.s2,s2))return false;
        return true;
    }
};
ansStruct ans[maxn];
ansStruct reala[maxn];
void init_score_table()
{
    score['s']=score['i']=score['e']=1;
    score['r']=score['t']=score['a']=score['n']=2;
    score['o']=score['l']=3;
    score['u']=score['d']=score['c']=4;
    score['y']=score['p']=score['g']=score['h']=score['b']=score['m']=5;
    score['w']=score['k']=score['f']=score['v']=6;
    score['q']=score['j']=score['z']=score['x']=7;
}
bool init()
{
    if(fin>>ds)
    {
        icount_ans=idic=0;
        int y=1,tlen;
        char temps[10];
        len=strlen(ds);
        memset(r,0,sizeof(r));
        for(int i=0; i<len; i++)
        r[ds[i]]++;
        dic_fin>>temps;
        while(temps[0]!='.')
        {
            y=1;
            memset(tr,0,sizeof(tr));
            tlen=strlen(temps);
            for(int i=0; i<tlen; i++)
            {
                tr[temps[i]]++;
                if(tr[temps[i]]>r[temps[i]])
                {
                    y=0;
                    break;
                }
            }
            if(y)
            strcpy(dic[idic++],temps);
            dic_fin>>temps;
        }
        fin.close();
        dic_fin.close();
        init_score_table();
        return true;
    }
    return false;
}
void mySort(ansStruct r[],int n)
{
    ansStruct ts;
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<n-1-i; j++)
        {
            int r1=strcmp(r[j].s1,r[j+1].s1);
            if(r1)
            {
                if(r1>0)
                {
                    ts=r[j];
                    r[j]=r[j+1];
                    r[j+1]=ts;
                }
            }
            else
            {
                r1=strcmp(r[j].s2,r[j+1].s2);
                if(r1>0)
                {
                    ts=r[j];
                    r[j]=r[j+1];
                    r[j+1]=ts;
                }
            }
        }
    }
}
void slove()
{
    int tj[130],tlen,i,j,e;
    int temp_max_score=0;
    for(i=0; i<idic; i++)
    {
        strcpy(ans[icount_ans++].s1,dic[i]);
    }
    int tt=icount_ans;
    for(i=0; i<tt; i++)
    {
        memset(tr,0,sizeof(tr));
        tlen=strlen(ans[i].s1);
        for(e=0; e<tlen; e++)
        tr[ans[i].s1[e]]++;
        for(j=0; j<idic; j++)
        {
            for(e='a'; e<='z'; e++)
            tj[e]=tr[e];
            tlen=strlen(dic[j]);
            for(e=0; e<tlen; e++)
            {
                char ch=dic[j][e];
                if(++tj[ch]>r[ch])
                break;
            }
            if(e==tlen)
            {
                if(strlen(ans[i].s2)==0)
                {
                    strcpy(ans[i].s2,dic[j]);
                    ans[i].cal_score();
                    if(ans[i].sco>temp_max_score)
                    temp_max_score=ans[i].sco;
                }
                else
                {
                    ans[icount_ans]=ans[i];
                    strcpy(ans[icount_ans].s2,dic[j]);
                    icount_ans++;
                }
            }
        }
    }
    int max_score=0;
    for(i=0; i<icount_ans; i++)
    {
        ans[i].cal_score();
        ans[i].ss();
        if(ans[i].sco>max_score)
        {
            max_score=ans[i].sco;
        }
    }
    int ir=0;
    for(int i=0; i<icount_ans; i++)
    {
        if(ans[i].sco==max_score)
        reala[ir++]=ans[i];
    }
    mySort(reala,ir);
    zout<<max_score<<endl;
    ansStruct la;
    for(i=0; i<ir; i++)
    {
        if(reala[i].equ(la))continue;
        reala[i].print();
        la=reala[i];
    }
}
int main()
{
    while(init())
    slove();
    return 0;
}


 类似资料:

相关阅读

相关文章

相关问答