第一屆C語言比賽答案
01-09
第一次C語言比賽完美落幕
嵌入式Linux:第一屆「C語言比賽」題目鏈接
提示 - C語言網密碼 2580
下面給出題目解析和答案
題目1
題目描述
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.題目翻譯
輸入一個數字 N 和一個長字元串 XXXXXXXXXX
然後輸入 N個子字元串TTTTT,並給出每個子字元串的權值Y
然後輸出長字元串的最大權值 S
答案提供者
參考代碼
#include<stdio.h>
#include<string.h>
#define MAX_STR_LEN 10001
#define MAX_TRIE_ENTRY_LEN 31
#define MAX_TRIE_NODE 300001
#define MAX_ALPHA 26
int g_trie[MAX_TRIE_NODE][MAX_ALPHA]; /* 字典樹 */
int trie_vals[MAX_TRIE_NODE]; /* 字典樹上關鍵字權值 */
int curr_node = 0; /* 字典樹當前分配的節點編號 */
void build_trie(char *s, int val) /* 插入單詞S到字典樹中 */
{
int root = 0;
char *p = s;
int num = 0;
while (*p !=