您的位置:首页 > 其它

USACO 4.3 Letter game

2012-01-23 10:54 337 查看
今天年初一,到邻居和临近亲戚家转完回来接着做昨晚未写完的USACO 4.3 letter game,此题给出的字典最多有40000个单词,但是仔细算一下可以发现有很多用处不大,我们来看一下:由于最多只给出7个字母,最少也要给出3个,每个单词也是这个样子(这两个条件非常关键),所以如果出现pair words,那么只有2种情况——长度3的单词+长度3的单词,长度3+长度4。而长度为4的合法单词最多只有7^4=2401个,所以在查找pair words时大部分单词是可以被筛掉的。一个单词的情况当然非常简单了,扫描一下就可以了。注意给出的单词不必全都用上,因为这里WA了= =。此题作为新年为自己奉上的一个礼物吧,呵呵,虽然是个小水题。

/*
ID: zlqest11
LANG: C++
TASK: lgame
*/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

const int N = 40005;
const int pv[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};

struct node{
int first, second;
node(){
first = second = 0;
}
node(int x, int y){
first = x, second = y;
}
};

char latter[10], dic
[10];
int n, len, val
;
int ct[26], tmp[26];

vector <int> hash3[50], hash4[50];
vector <node> res;

bool cmp(const node &a, const node &b){
int x = strcmp(dic[a.first], dic[b.first]);
int y = strcmp(dic[a.second], dic[b.second]);
if(x == 0)  return  y < 0;
else  return  x < 0;
}

int getVal(const char * str){
int ans = 0, len = strlen(str);
for(int i = 0; i < len; i++)
ans += pv[str[i]-'a'];
return  ans;
}

bool check(const char * str){
for(int i = 0; str[i]; i++){
bool flag = false;
for(int j = 0; j < len; j++)
if(str[i] == latter[j]){
flag = true;
break;
}
if(!flag)  return  false;
}
return  true;
}

bool Yes(int x, int y){
memset(tmp, 0, sizeof(tmp));
int lx, ly;
lx = strlen(dic[x]);
ly = strlen(dic[y]);
for(int i = 0; i < lx; i++)  tmp[dic[x][i]-'a']++;
for(int i = 0; i < ly; i++)  tmp[dic[y][i]-'a']++;
for(int i = 0; i < 26; i++)
if(ct[i] < tmp[i])
return  false;
return  true;
}

void fuc(const char * str, int pos){
int ans = 0, len = strlen(str);
if(!(len==3 || len==4))  return;
ans = val[pos];
if(len == 3)  hash3[ans].push_back(pos);
if(len == 4)  hash4[ans].push_back(pos);
}

int main()
{
char str[10];
//read letter set
freopen("lgame.in", "r", stdin);
scanf("%s", latter);
len = strlen(latter);
memset(ct, 0, sizeof(ct));
for(int i = 0; i < len; i++){
ct[latter[i]-'a']++;
}
fclose(stdin);
freopen("lgame.dict", "r", stdin);
freopen("lgame.out", "w", stdout);
//init the dic
n = 0;
while(~scanf("%s", str)){
if(strcmp(str, ".") == 0)  break;
else if(check(str)){
++n;
strcpy(dic
, str);
val
= getVal(dic
);
}
}
//init hash
for(int i = 0; i < 50; i++){
hash3[i].clear();
hash4[i].clear();
}
strcpy(dic[0], "");
for(int i = 1; i <= n; i++)  fuc(dic[i], i);
int ans;
bool flag = false;
//find ans and generate case
for(ans = 49; ans >= 0; ans--){
for(int i = 1; i <= n; i++){
if(Yes(i, 0) && val[i]==ans){
flag = true;
res.push_back(node(i, 0));
}
}
for(int i = 1; i <= ans; i++)
if(hash3[i].size() != 0){
int index = ans-i;
if(len==7 && hash4[index].size()!=0){
for(int p = 0; p < hash3[i].size(); p++)
for(int q = 0; q < hash4[index].size(); q++){
if(Yes(hash3[i][p], hash4[index][q])){
res.push_back(node(hash3[i][p], hash4[index][q]));
flag = true;
}
}
}
if(len>=6 && hash3[index].size()!=0){
for(int p = 0; p < hash3[i].size(); p++)
for(int q = 0; q < hash3[index].size(); q++){
if(Yes(hash3[i][p], hash3[index][q])){
res.push_back(node(hash3[i][p], hash3[index][q]));
flag = true;
}
}
}
}
if(flag)  break;
}
for(int i = 0; i < res.size(); i++){
if(res[i].second == 0)  continue;
else{
if(strcmp(dic[res[i].first], dic[res[i].second]) > 0)
swap(res[i].first, res[i].second);
}
}
sort(res.begin(), res.end(), cmp);
printf("%d\n", ans);
for(int i = 0; i < res.size(); i++){
if(i && res[i].first==res[i-1].first && res[i].second==res[i-1].second)
continue;
if(res[i].second == 0)  printf("%s\n", dic[res[i].first]);
else  printf("%s %s\n", dic[res[i].first], dic[res[i].second]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: