Uvalive 4260 Fortune Card Game (Regionals-Asia Taipei +DP)

Source: Internet
Author: User

"Topic link": Click here~~

"To the effect of the topic":


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 (the "the figure") is listed as the address of the card. A symbol is drawn beside the address. There is five kinds of symbols, which is listed below the card. For convenience, the let each symbol is represented by an 中文版 letter from ' A '-' E '. The bottom of a card contains another number called "Next Fortune number."

Figure 1: A sample Fortune card and symbols.

In a set of Fortune cards, many cards can has same address; That is, address-is-not limited-appear only in one card. However, there'll be is no cards that is identical, i.e., No. Cards with same address, symbol, and next fortune number.

The Fortune card game is played as follows. A player starts with cards that has address 1. The goal of the game is trying to complete a ' spell ', that's composed by the symbols. For example, let a spell is ' Baddad '. In the first move, the player would look for cards this has address 1 with a star symbol (which matches ' B ' in the spell). The next fortune numbers of these cards is the new addresses for the next move. The player can select one card to advance to a new address X. The selected card is and put back to the cards for next move, but the fortune number was written down.

Let the example be continued. In the next move, the player needs-to-look for the cards that has the 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 has been written down.

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


Technical specification


N-the number of test cases, N10. C-the number of cards, C800. L-the length of a spell, L150.

Input

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

Output

For each test case, please output the maximum score that can is 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

 
5
"Main idea": the player controls a card game, n cards, each with three attributes, Ai: Address, S: Card letter, BI: Card value, give you a fixed spell phonetic Alphabet sequence, to integrate all phonetic Alphabet sequence to get the maximum value, rules, if you choose a card, Then the value attribute of the card is the address attribute of the next sought card.

"Thinking" game when the problem is too urgent, some key parts are not clear, resulting in the final commissioning for a long time, after the game found that there is actually more simple, violent practice: the definition of dp[i][j]: Indicates that the first card property is the maximum value of J, there is a recurrence relationship: Dp[i][st[j].bi]=max (dp[ I][ST[J].BI],DP[I-1][ST[J].AI]+ST[J].BI);

The next fortune numbers of these cards is the new addresses for the next move
Note that the choice of the card's value attribute as the next card address, key points explained Dp[i-1][st[j].ai]+st[j].bi this state transfer

Code:

/* * problem:uvalive 4260 * Running time:46ms * complier:g++ * Author:javaherongwei * Create time:20:16 2015/10/14 Week
Tri */#include <bits/stdc++.h> using namespace std;
const int MAXN=1E3+10;
const int inf=0x3f3f3f3f; inline int max (int a,int b) {return a>b?a:b;} inline int min (int a,int b) {return a<b?a:b;} struct Node {int ai,b
    I
char s;
} ST[MAXN];
String str;
int DP[MAXN][MAXN];
    int main () {//freopen ("2.txt", "R", stdin);
    int t;cin>>t;
        while (t--) {int n;cin>>n;
        for (int i=1; i<=n; ++i) {cin>>st[i].ai>>st[i].s>>st[i].bi;
        } cin>>str;
        int len=str.size ();
        Memset (Dp,-inf,sizeof (DP)); DP[0][1]=0;///DP[I][J]: Represents the maximum value of the current card value attribute j for (int i=1; i<=len; ++i) {///enumeration result alphabetic sequence for (int j=1; j<= N ++J) {////enum J card if (St[j].s!=str[i-1]) continue;///If the card currently enumerated does not correspond to a card in spell, continue dp[i][st[ J].bi]=max (DPI
               [St[j].bi],dp[i-1][st[j].ai]+st[j].bi];
            cout<< "dp[i][j]=" <<dp[i][j]<<endl;
        }} int ans=0;
        for (int i=1; i<=n; ++i) Ans=max (Ans,dp[len][i]);
    printf ("%d\n", ans);
} return 0; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.