HDU 2296 Ring (AC自动机 + dp[类似于背包])

来源:互联网 发布:新疆网络安全形势 编辑:程序博客网 时间:2024/06/06 02:45

题意:

给你m 个喜欢的串, 每个串都有一个权值, 一个字符串的权值为 每个喜欢的串的权值 乘以 出现次数。 问你长度为1~n 的字符串中 权值最大的串是什么, 权值一样输出长度最短,长度一样输出字典序最小。

思路:

匹配多种串, 显然ac自动机。

答案要求权值最大, 权值一样长度最小, 长度一样 字典序最小。 显然是那种类似于背包的套路。 用int dp[][] 记录答案, string path[][] 记录路径即可(就可以找到长度和字典序)


令dp[i][j] 表示目前走到串的第i 位, 在状态j 的权值数, 同时, path[i][j] 对应着dp 的路径。

更新答案,按照题目描述更新即可。


简单说一下 新加一位如和匹配。

新加一位的话, 我们直接遍历这个节点的所有fail 指针, 看看哪些出现了,哪些没出现。  加上权值即可, 每次都是处理后缀, 不会重复计算。

(当然这个所有fail指针的权值和 可以预处理, 但是我预处理了反而更慢了= =[maybe 评测机的问题。。。])

#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <iostream>#include <string>using namespace std;const int maxn = 1100 + 3;int a[51];const int inf = 0x3f3f3f3f;struct Trie{    int L, root;    int next[maxn][26];    int fail[maxn];    int flag[maxn]; /// 这个节点是否有喜欢的单词, 有的话记录权值。    int dp[51][maxn];    int sum[maxn];    string path[51][maxn];    void init(){        L = 0;        root = newnode();    }    int newnode(){        for (int i = 0; i < 26; ++i){            next[L][i] = -1;        }        flag[L] = 0;        sum[L] = 0;        return L++;    }    void insert(char* s,int vall){        int len = strlen(s);        int nod = root;        for (int i = 0; i < len; ++i){            int id = s[i] - 'a';            if (next[nod][id] == -1){                next[nod][id] = newnode();            }            nod = next[nod][id];        }        flag[nod] = vall;    }    void bfs(){        fail[root] = root;        queue<int>q;        for (int i = 0; i < 26; ++i){            if (next[root][i] == -1){                next[root][i] = root;            }            else {                fail[next[root][i] ] = root;                q.push(next[root][i]);            }        }        while(!q.empty()){            int u = q.front(); q.pop();            for (int i = 0; i < 26; ++i){                if (next[u][i] == -1){                    next[u][i] = next[fail[u] ][i];                }                else {                    fail[next[u][i] ] = next[fail[u] ][i];                    q.push(next[u][i]);                }            }        }        for (int i = 0; i < L; ++i){ ///  预处理所有节点fail指针的权值和(即后缀权值和) (当然可能不预处理更快。。= =)            int tmp = i;            while(tmp != root){                sum[i] += flag[tmp];                tmp = fail[tmp];            }        }    }    void solve(int n){        memset(dp,-1,sizeof dp);        path[0][0] = "";        dp[0][0] = 0;        for (int i = 1; i <= n; ++i){            for (int j = 0; j < L; ++j){                for (int k = 0; k < 26; ++k){                    int nx = next[j][k];                    int tmp = nx;                    if (dp[i-1][j] == -1) continue;                    int vv = sum[nx];                    if (dp[i-1][j] + vv > dp[i][nx]){ /// 按照题目描述三种判断即可                        dp[i][nx] = dp[i-1][j] + vv;                        char ch = 'a' + k;                        path[i][nx] = path[i-1][j] + ch;                    }                    else if (dp[i][nx] == dp[i-1][j] + vv){                        if (path[i-1][j].length() + 1 < path[i][nx].length()){                            char ch = 'a' + k;                            path[i][nx] = path[i-1][j] + ch;                        }                        else if (path[i-1][j].length() + 1 == path[i][nx].length()){                            char ch = 'a' + k;                            string newpath = path[i-1][j] + ch;                            if (newpath < path[i][nx])                                path[i][nx] = newpath;                        }                    }                }            }        }        string anspath = "";        int ans = 0;        for (int j = 1; j <= n; ++j){            for (int i = 0; i < L; ++i){                if (dp[j][i] > ans){                    ans = dp[j][i];                    anspath = path[j][i];                }                else if (dp[j][i] == ans){                    if (path[j][i].length() < anspath.length()){                        anspath = path[j][i];                    }                    else if (path[j][i].length() == anspath.length()){                        if (path[j][i] < anspath){                            anspath = path[j][i];                        }                    }                }            }        }        printf("%s\n", anspath.c_str());    }}ac;char s[103][13];int main(){    int T;    scanf("%d",&T);    while(T--){        int n, m;        scanf("%d %d",&n, &m);        ac.init();        for (int i = 0; i < m; ++i){            scanf("%s", s[i]);        }        for (int i = 0; i < m; ++i){            scanf("%d", &a[i]);            ac.insert(s[i], a[i]);        }        ac.bfs();        ac.solve(n);    }    return 0;}

Ring

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3872    Accepted Submission(s): 1280


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

 

Input
The input consists of several test cases. The first line of input consists of an integer T, indicating the number of test cases. Each test case starts with a line consisting of two 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 each 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 is less than 11 and bigger than 0.
4. 1 ≤ Hi ≤ 100. 
5. All the words in the input are different.
6. All the words just consist of 'a' - 'z'.
 

Output
For each test case, output the string to engrave on a single line.
If there's more than one possible answer, first output the shortest one. If there are still multiple solutions, output the smallest in lexicographically order.

The answer may be an empty string. 
 

Sample Input
27 2loveever5 55 1ab5
 

Sample Output
loveverabab
Hint
Sample 1: weight(love) = 5, weight(ever) = 5, so weight(lovever) = 5 + 5 = 10Sample 2: weight(ab) = 2 * 5 = 10, so weight(abab) = 10
 

Source
The 4th Baidu Cup final
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  3247 3341 2825 2896 2243