USACO4.3.3 Letter Game (lgame)

来源:互联网 发布:信阳师范学院网络教学 编辑:程序博客网 时间:2024/05/01 15:42
从字典里筛选出所有符合条件的串。

看已经没剩几个了,枚举就行。



/*ID:xsy97051LANG:C++TASK:lgame*/#include <fstream>#include <cstring>using namespace std; ifstream cin1("lgame.in");ifstream cin2("lgame.dict");ofstream cout("lgame.out");int main(){    string str,dat[40001];    int maxr=0,num[26],temp[26],tt=0,sc[26]={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};    memset(num,0,sizeof(num));    cin1>>str;    for (int i=0;i<str.size();i++) num[str[i]-'a']++;    cin2>>str;    while (str!=".")    {          bool okay=1;          memset(temp,0,sizeof(temp));          for (int i=0;i<str.size();i++) temp[str[i]-'a']++;          for (int i=0;i<26;i++) if (temp[i]>num[i]){              okay=0;break;          }          if (okay) dat[++tt]=str;          cin2>>str;    }    dat[++tt]="";    for (int i=1;i<tt;i++)        for (int j=i+1;j<=tt;j++)        {            memset(temp,0,sizeof(temp));            for (int k=0;k<dat[i].size();k++)                temp[dat[i][k]-'a']++;            for (int k=0;k<dat[j].size();k++)                temp[dat[j][k]-'a']++;            bool okay=1;            int sum=0;            for (int k=0;k<26;k++)                if (temp[k]>num[k]){                                    okay=0;                                    break;                                    }            if (!okay) continue;            for (int k=0;k<26;k++)                sum+=sc[k]*temp[k];            if (sum>maxr) maxr=sum;        }    cout<<maxr<<endl;    for (int i=1;i<tt;i++)        for (int j=i+1;j<=tt;j++)        {            memset(temp,0,sizeof(temp));            for (int k=0;k<dat[i].size();k++)                temp[dat[i][k]-'a']++;            for (int k=0;k<dat[j].size();k++)                temp[dat[j][k]-'a']++;            bool okay=1;            int sum=0;            for (int k=0;k<26;k++)                if (temp[k]>num[k]){                                    okay=0;                                    break;                                    }            if (!okay) continue;            for (int k=0;k<26;k++)                sum+=sc[k]*temp[k];            if (sum==maxr) {                           cout<<dat[i];                           if (dat[j]!="") cout<<" "<<dat[j];                           cout<<endl;                           }        }        return 0;}


0 0
原创粉丝点击