日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[USACO12JAN]Video Game Combos

發布時間:2023/12/29 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [USACO12JAN]Video Game Combos 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

AC自動機建立fail樹后樹上DP

# include <stdio.h> # include <stdlib.h> # include <iostream> # include <string.h> # include <algorithm> # include <queue> # define IL inline # define RG register # define ll long long # define Fill(a, b) memset(a, b, sizeof(a)) using namespace std;IL ll Read(){RG char c = getchar(); RG ll x = 0, z = 1;for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + c - '0';return x * z; }const int MAXN(1010); struct AC{int fail, ch[3], num; } tree[MAXN]; int n, cnt, k, f[MAXN][MAXN], ans; char t[MAXN]; queue <int> Q;IL void Insert(){RG int len = strlen(t), x = 0;for(RG int i = 0; i < len; i++){if(!tree[x].ch[t[i] - 'A']) tree[x].ch[t[i] - 'A'] = ++cnt;x = tree[x].ch[t[i] - 'A'];}tree[x].num++; }IL void Bfs(){while(!Q.empty()) Q.pop();for(RG int i = 0; i < 3; i++)if(tree[0].ch[i]) Q.push(tree[0].ch[i]);while(!Q.empty()){RG int u = Q.front(); Q.pop();for(RG int i = 0; i < 3; i++)if(tree[u].ch[i]){tree[tree[u].ch[i]].fail = tree[tree[u].fail].ch[i];Q.push(tree[u].ch[i]);}else tree[u].ch[i] = tree[tree[u].fail].ch[i];tree[u].num += tree[tree[u].fail].num;} }int main(){n = Read(); k = Read();for(RG int i = 1; i <= n; i++){scanf(" %s", t);Insert();}Bfs();Fill(f, -127); f[0][0] = 0;for(RG int K = 1; K <= k; K++){for(RG int i = 0; i <= cnt; i++)for(RG int j = 0; j < 3; j++)f[K][tree[i].ch[j]]=max(f[K][tree[i].ch[j]],f[K - 1][i] + tree[tree[i].ch[j]].num);}for(RG int i = 0; i <= cnt; i++)ans = max(ans, f[k][i]);printf("%d\n", ans);return 0; }

轉載于:https://www.cnblogs.com/cjoieryl/p/8206389.html

總結

以上是生活随笔為你收集整理的[USACO12JAN]Video Game Combos的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。