Ring 【HDU - 2296】【AC自动机+静态+DP】

题目链接

之前写的题解报告,第二次写了,多了些理解了

——能再多给点AC吗?不止是题目…… 


  有段时间没有去碰AC自动机了,于是乎写了这道题(其实是放松一下心态之用的,最近好多的大起大落……QAQ)

  无非就是在这道题的时候,注意一下,重叠也是有效的,所以要考虑到重合的情况,也就是下推fail指针的时候,要去注意重叠的部分,向上加就是了。

  然后就是一个DP[长度][状态(自动机上的状态)],用来推状态,接下去用str[][][]来继承即可。


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(a, b) make_pair(a, b)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
struct node
{
    int nex[26], val, fail;
    node() { memset(nex, 0, sizeof(nex)); val = fail = 0; }
    void clear() { memset(nex, 0, sizeof(nex)); val = fail = 0; }
}a[1005];
int N, M, tot;
char virus[105][12];
void insert(int ti, int val)
{
    int temp = 0, len = (int)strlen(virus[ti]);
    for(int i=0; i<len; i++)
    {
        int _id = virus[ti][i] - 'a';
        if(!a[temp].nex[_id])
        {
            a[++tot].clear();
            a[temp].nex[_id] = tot;
        }
        temp = a[temp].nex[_id];
    }
    a[temp].val += val;
}
void build_fail()
{
    queue<int> Q;   Q.push(0);
    int tmp, p, son;
    while(!Q.empty())
    {
        tmp = Q.front();    Q.pop();
        for(int i=0; i<26; i++)
        {
            son = a[tmp].nex[i];
            if(son)
            {
                if(!tmp) a[son].fail = 0;
                else
                {
                    p = a[tmp].fail;
                    while(p && !a[p].nex[i]) p = a[p].fail;
                    a[son].fail = a[p].nex[i];
                }
                if(a[a[son].fail].val) a[son].val += a[a[son].fail].val;
                Q.push(son);
            }
            else a[tmp].nex[i] = a[a[tmp].fail].nex[i];
        }
    }
}
int dp[55][1005];
char ans[55], s[55], str[55][1005][55];
void solve()
{
    strcpy(ans, "\0");  strcpy(str[0][0], "\0");
    memset(dp, -1, sizeof(dp));
    dp[0][0] = 0;
    for(int len=1; len<=N; len++)
    {
        for(int i=0; i<=tot; i++)
        {
            if(dp[len-1][i] == -1) continue;
            strcpy(s, str[len-1][i]);   s[len] = '\0';
            for(int j=0, son; j<26; j++)
            {
                s[len - 1] = 'a' + j;
                son = a[i].nex[j];
                if(dp[len][son] < dp[len-1][i] + a[son].val)
                {
                    dp[len][son] = dp[len-1][i] + a[son].val;
                    strcpy(str[len][son], s);
                }
                else if(dp[len][son] == dp[len-1][i] + a[son].val && strcmp(str[len][son], s) > 0) strcpy(str[len][son], s);;
            }
        }
    }
    int maxx = 0;
    for(int i=1; i<=N; i++)
    {
        for(int j=1; j<=tot; j++)
        {
            if(dp[i][j] > maxx)
            {
                maxx = dp[i][j];
                strcpy(ans, str[i][j]);
            }
            else if(dp[i][j] == maxx && strlen(ans) > strlen(str[i][j])) strcpy(ans, str[i][j]);
            else if(dp[i][j] == maxx && strlen(ans) == strlen(str[i][j]) && strcmp(ans, str[i][j]) > 0) strcpy(ans, str[i][j]);
        }
    }
    printf("%s\n", ans);
}
inline void init()
{
    tot = 0;
    a[0].clear();
}
int main()
{
    int T;  scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d", &N, &M);
        init();
        for(int i=1; i<=M; i++) scanf("%s", virus[i]);
        for(int i=1, e1; i<=M; i++) { scanf("%d", &e1); insert(i, e1); }
        build_fail();
        solve();
    }
    return 0;
}

 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值