English Game

English Game

时间: 1ms        内存:128M

描述:

This English game is a simple English words connection game.

The rules are as follows: there are N English words in a dictionary, and every word has its own weight v. There is a weight if the corresponding word is used. Now there is a target string X. You have to pick some words in the dictionary, and then connect them to form X. At the same time, the sum weight of the words you picked must be the biggest.

输入:

There are several test cases. For each test, N (1<=n<=1000) and X (the length of x is not bigger than 10000) are given at first. Then N rows follow. Each row contains a word wi (the length is not bigger than 30) and the weight of it. Every word is composed of lowercases. No two words in the dictionary are the same.

输出:

For each test case, output the biggest sum weight, if you could not form the string X, output -1.

示例输入:

1 aaaa
a 2
3 aaa
a 2
aa 5
aaa 6
4 abc
a 1
bc 2
ab 4
c 1
3 abcd
ab 10
bc 20
cd 30
3 abcd
cd 100
abc 1000
bcd 10000

示例输出:

8
7
5
40
-1

提示:

参考答案(内存最优[800]):

#include<stdio.h>

int fn[30][2]={

	220,284,

	1184,1210,

	2620,2924,

	5020,5564,

	

	6232,6368,

	10744,10856,

	12285,14595,

	17296,18416,

	

	63020,76084,

	66928,66992,

	67095,71145,

	69615,87633,

	

	79750,88730,

	100485,124155,

	122265,139815,

	

	122368,123152,

	141664,153176,

	142310,168730,

	171856,176336,

	

	176272,180848,

	185368,203432,

	196724,202444,

	280540,365084,

	

	308620,389924,

	319550,430402,

	356408,399592,

	437456,455344,

	

	469028,486178

};

int main()

{

	int a, b, i, count;

	//freopen("in.in", "r", stdin);

	//freopen("out.out", "w", stdout);

	while(scanf("%d%d", &a, &b) != EOF)

	{

		for(count=0, i=0; i<28; i++)

			if(a<=fn[i][0]&&fn[i][1]<=b)

				count++;

		printf("%d\n",count);

	} 

	return 0;	

} 

参考答案(时间最优[20]):

#include<stdio.h>

int fn[30][2]={

	220,284,

	1184,1210,

	2620,2924,

	5020,5564,

	

	6232,6368,

	10744,10856,

	12285,14595,

	17296,18416,

	

	63020,76084,

	66928,66992,

	67095,71145,

	69615,87633,

	

	79750,88730,

	100485,124155,

	122265,139815,

	

	122368,123152,

	141664,153176,

	142310,168730,

	171856,176336,

	

	176272,180848,

	185368,203432,

	196724,202444,

	280540,365084,

	

	308620,389924,

	319550,430402,

	356408,399592,

	437456,455344,

	

	469028,486178

};

int main()

{

	int a, b, i, count;

	//freopen("in.in", "r", stdin);

	//freopen("out.out", "w", stdout);

	while(scanf("%d%d", &a, &b) != EOF)

	{

		for(count=0, i=0; i<28; i++)

			if(a<=fn[i][0]&&fn[i][1]<=b)

				count++;

		printf("%d\n",count);

	} 

	return 0;	

} 

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注