HDU 2296 ac automaton +DP (get the most valuable string)

Source: Internet
Author: User

Ring

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3180 Accepted Submission (s): 1033


Problem Descriptionfor The hope of a forever love, Steven was planning to send a ring to Jane with a romantic string Engrav Ed on. The string ' s length should not exceed N. The careful Steven knows Jane so deeply that he knows she favorite words, such as "Love", "Forever". Also, he knows the value of each word. The higher value a word has the more joy Jane would get when see it.
The weight of a word is defined as it appeared times in the romantic string multiply by it value, while the weight of th E romantic string is defined as the sum of all words ' weight. You should output of the string making its weight maximal.

Inputthe input consists of several test cases. The first line of input consists of an integer T, indicating the number of the test cases. Each test case starts with a line consisting of integers:n, M, indicating the string ' s length and the number of Jane ' s favorite words. Each of the following M lines consists of a favorite word Si. The last line of all test case consists of M integers, while the i-th number indicates the value of Si.
Technical specification

1. t≤15
2.0 < n≤50, 0 < m≤100.
3. The length of each word was less than and bigger than 0.
4.1≤hi≤100.
5. All the words in the input is different.
6. All the words just consist of ' a '-' Z '.

Outputfor Each test case, the output of the string to engrave on a.
If there ' s more than one possible answer, first output the shortest one. If There is still multiple solutions, output the smallest in lexicographically order.

The answer is an empty string.

Sample Input27 2loveever5 1AB5

Sample Outputloveverabab

/*hdu 2296 ac automaton +DP (get the most value of the string) give you M-strings, each substring has its own value, allowing you to find the most valuable string with a length of less than or equal to N. Requires the length of the string to be as small as possible, and the length of the dictionary sequence is the smallest in the generation of the state transition diagram, DP thinking solution. Use Dp[i][j] to record the maximum value when the length is I and the state is J, while the string is recorded with str[i][j][55] when the value is the same, the string is compared. hhh-2016-04-24 17:13:36*/#include <iostream> #include <vector> #include <cstring> #include <string> #include <cstdio># Include <queue> #include <algorithm> #include <functional> #include <map>using namespace std;#  Define Lson (i<<1) #define Rson ((i<<1) | |) typedef unsigned LONG long ll;typedef unsigned int ul;const int mod = 20090717;const int INF = 0x3f3f3f3f;const int N = 12*105;int tot;int n,m;char tp[55];int Dp[55][n];char];    struct matrix{int len;    int ma[111][111];    Matrix () {};    Matrix (int L) {len = L;    }};int Compare (char A[],char b[]) {int len1 = strlen (a);    int len2 = strlen (b);    if (len1! = len2) return len1 > Len2; Return strcmp (A, b);}    struct tire{int nex[n][26],fail[n],ed[n]; int Root,l;        int NewNode () {for (int i = 0; i <; i++) nex[l][i] = 1;        ed[l++] =-1;    return L-1;        } void Ini () {L = 0,root = NewNode ();    memset (ed,-1,sizeof (ed));        } int cal (char ch) {if (ch = = ' A ') return 0;        else if (ch = = ' C ') return 1;        else if (ch = = ' G ') return 2;    else if (ch = = ' T ') return 3;        } void Inser (char buf[],int val) {int len = strlen (BUF);        int now = root;            for (int i = 0; i < len; i++) {int ta = buf[i]-' a ';            if (nex[now][ta] = =-1) nex[now][ta] = NewNode ();        now = Nex[now][ta];    } Ed[now] = val;        } void Build () {Queue<int >q;        Fail[root] = root;            for (int i = 0; i < i++) if (nex[root][i] = = 1) nex[root][i] = root; else {Fail[nex[root][i]] = root;            Q.push (Nex[root][i]);            } while (!q.empty ()) {Int. now = Q.front ();            Q.pop ();//if (Ed[fail[now]])//Ed[now] = Ed[fail[now]]; for (int i = 0; i < i++) {if (nex[now][i] = = 1) nex[now][i] = Nex[fail [Now]]                [i];                    else {Fail[nex[now][i]] = nex[fail[now]][i];                Q.push (Nex[now][i]);        }}}} Matrix To_mat () {Matrix mat (L);        memset (mat.ma,0,sizeof (mat.ma)); for (int i = 0, i < L; i++) {for (int j = 0; J < 4; J + +) {if (!ed[nex[i][            J]]) Mat.ma[i][nex[i][j]] + +;    }} return mat;                } void Solve () {for (int j = 0; J <= N; j + +) {for (int i = 0; i < n; i++)        Dp[j][i] =-1;  }      Dp[0][0] = 0;        Char tan[55] = {""};        int tMax = 0;        strcpy (Ans[0][0], "");        strcpy (TP, "");                 for (int i = 1, i <= N; i++) for (int j = 0; J < N; j + +) {if (Dp[i-1][j] >= 0)                    {strcpy (tp,ans[i-1][j]);                    int len = strlen (TP);                        for (int k = 0; k < k++) {int t= dp[i-1][j];                        if (Ed[nex[j][k]] > 0) T + = ed[nex[j][k]];                        Tp[len] = ' a ' +k;                        Tp[len+1] = 0; if (T > dp[i][nex[j][k]) | |                        (t = = Dp[i][nex[j][k]] && Compare (ANS[I][NEX[J][K]],TP) > 0))                            {strcpy (ANS[I][NEX[J][K]],TP);                        Dp[i][nex[j][k]] = t; } if (T >tmax | | (TMax = = t && Compare (TAN,TP);                            0)) {tMax = t;                        strcpy (TAN,TP);        }}}}//printf ("%d\n", TMax);    printf ("%s\n", tan); }};    Tire Ac;char Buf[105][12];int Main () {int T;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n,&m);        Ac.ini ();        for (int i = 0; i < m; i++) {scanf ("%s", Buf[i]);        } int x;            for (int i = 0; i < m; i++) {scanf ("%d", &x);        Ac.inser (BUF[I],X);        } ac.build ();    Ac.solve (); } return 0;}

  

HDU 2296 ac automaton +DP (get the most valuable string)

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.