uva 4260 - Fortune Card Game 【dp】

题目链接:uva 4260 - Fortune Card Game

A popular card game called fortune" is getting popular in country X. Fig. 1 shows one of the cards. In each card, a positive integer number (20 in the figure) is listed as the address of the card. A symbol is drawn beside the address. There are five kinds of symbols, which are listed below the card. For convenience, let each symbol be represented by an English letter from `A'-`E'. The bottom of a card contains another number callednext fortune number.”

\epsfbox{p4260.eps}
Figure 1: A sample fortune card and symbols.
In a set of fortune cards, many cards can have same address; that is, address 20 is not limited to appear only in one card. However, there will be no cards that are identical, i.e., no two cards with same address, symbol, and next fortune number.

The fortune card game is played as follows. A player starts with cards that have address 1. The goal of the game is trying to complete a spell" that is composed by the symbols. For example, let a spell beBADDAD”. In the first move, the player will look for cards that have address 1 with a star symbol (which matches `B’ in the spell). The next fortune numbers of these cards are the new addresses for the next move. The player can select one card to advance to a new address x. The selected card is then put back to the cards for next move but the fortune number is written down.

Let the example be continued. In the next move, the player needs to look for the cards that have new address x with the cross symbol (which matches the second `A’ in the spell). Again, the player selects one card to advance to another new address. This procedure continues until the spell “BADDAD” is completed. Once the player completes a spell, he wins a score by adding all the next fortune numbers of the selected card, which have been written down.

Given a spell and a set of fortune cards, please output the maximum score that can be played in this card game.

Technical Specification

N - the number of test cases, N 10 .
C - the number of cards, C 800 .
L - the length of a spell, L 150 .
Input

Test data begins with an integer N which is the number of test cases. Each test case begins with an integer C , which is the number of cards. Following the number C is C lines of card information. Each card is represented by (Address Symbol NextF ortuneNumber). The address and next fortune number are between 1 and 800. The symbols are capital letters from A' toE’. The last line of a test case is a spell. The spell is a string composed by capital letters from A' toE’. The length of the spell (L ) is less than 150.

Output

For each test case, please output the maximum score that can be collected for each test case.

Sample Input

2
7
1 A 2
1 A 3
2 A 3
2 B 4
2 B 5
3 A 3
3 B 4
AAAAB
6
1 A 2
1 B 2
1 A 3
1 B 3
2 A 3
2 B 3
AB
Sample Output

16
5

题意:给你n张卡片,每张卡片有三个信息——地址id、对应的字符op、下一链接地址val。而每张卡片的价值就是下一链接地址,让你构造出一个字符串。问你最大的价值。

思路:考虑dp
设置dp[i][j]处理到第i个字符且用第j张卡片匹配该字符。
if(id[k] == val[j] && op[k] == str[i+1]) dp[i+1][k] = max(dp[i][j] + val[k]);

AC代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
#define CLR(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 1000 + 10;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
struct Node {
    int id, val;
    char op[2];
};
Node num[MAXN];
char str[160];
int dp[160][900];
int main()
{
    int t; scanf("%d", &t);
    while(t--) {
        int n; scanf("%d", &n);
        for(int i = 1; i <= n; i++) {
            scanf("%d%s%d", &num[i].id, num[i].op, &num[i].val);
        }
        scanf("%s", str); int len = strlen(str);
        CLR(dp, 0);
        for(int j = 1; j <= n; j++) {
            if(num[j].op[0] == str[0] && num[j].id == 1) {
                dp[0][j] = num[j].val;
            }
        }
        for(int i = 0; i < len-1; i++) {
            for(int j = 1; j <= n; j++) {
                if(dp[i][j]) {
                    for(int k = 1; k <= n; k++) {
                        if(num[k].id == num[j].val && num[k].op[0] == str[i+1]) {
                            dp[i+1][k] = max(dp[i+1][k], dp[i][j] + num[k].val);
                        }
                    }
                }
            }
        }
        int ans = 0;
        for(int i = 1; i <= n; i++) {
            ans = max(ans, dp[len-1][i]);
        }
        printf("%d\n", ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值