HDU - 3247 Resource Archiver (AC自動機,狀壓dp)

\(\quad\)Great! Your new software is almost finished! The only thing left to do is archiving all your n resource files into a big one.
\(\quad\)Wait a minute… you realized that it isn’t as easy as you thought. Think about the virus killers. They’ll find your software suspicious, if your software contains one of the m predefined virus codes. You absolutely don’t want this to happen.
\(\quad\)Technically, resource files and virus codes are merely 01 strings. You’ve already convinced yourself that none of the resource strings contain a virus code, but if you make the archive arbitrarily, virus codes can still be found somewhere.
\(\quad\)Here comes your task (formally): design a 01 string that contains all your resources (their occurrences can overlap), but none of the virus codes. To make your software smaller in size, the string should be as short as possible.

Input

\(\quad\)There will be at most 10 test cases, each begins with two integers in a single line: n and m (2 <= n <= 10, 1 <= m <= 1000). The next n lines contain the resources, one in each line. The next m lines contain the virus codes, one in each line. The resources and virus codes are all non-empty 01 strings without spaces inside. Each resource is at most 1000 characters long. The total length of all virus codes is at most 50000. The input ends with n = m = 0.

Output

\(\quad\)For each test case, print the length of shortest string.

Sample Input

2 2
1110
0111
101
1001
0 0

Sample Output

5

題意

\(\quad\)就是給你\(n\)個需要的串和\(m\)個病毒串,最后讓你構造一個字符串,包含所有需要的串,不包括任何病毒串。

思路

\(\quad\)先講\(n+m\)個串全部插入ac自動機中,然后去構造\(fail\)指針的時候注意將\(fail\)節點的信息傳遞給子節點。
\(\quad\)首先容易想到\(dp[i][j]\)表示ac自動機上狀態為\(i\),包含需要串的狀態為\(j\)時所需要的最少字符串數。但是數據范圍\(i<=(略大于)5e4,j<=1024\),會\(MLE\)
\(\quad\)觀察數據范圍可以發現\(n<<m\),那么說明,在ac自動機上,無用的節點占大多數,那么就可以找出全部有用的節點,用\(bfs\)求出所有有用節點兩兩之間的最小距離,然后直接在這些有用的點上跑\(dp\)就可以了,那么可以得到新的\(dp\)方程。
\(\quad\)\(dp[i][j]\)表示到有用節點\(i\),包含需要串的狀態為\(j\)時所需要的最少字符長度。
\(\quad\)\(cnt[i]\)表示有用節點\(i\)上包含需要串的狀態。
\(\quad\)\(cc[i][j]\)表示從有用節點\(i\)走到有用節點\(j\)需要的最少字符數。
\(\quad\)\(dp[k][j|cnt[k]] = min(dp[k][cnt[k]],dp[i][j]+cc[i][k])\)
\(\quad\)最后在遍歷一遍\(dp[i][mx]\),就可以得到答案。

/***************************************************************> File Name    : a.cpp> Author       : Jiaaaaaaaqi> Created Time : 2019年04月29日 星期一 11時10分00秒***************************************************************/#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define  lowbit(x)  x & (-x)
#define  mes(a, b)  memset(a, b, sizeof a)
#define  fi         first
#define  se         second
#define  pii        pair<int, int>typedef unsigned long long int ull;
typedef long long int ll;
const int    maxn = 6e4 + 10;
const int    maxm = 1e5 + 10;
const ll     mod  = 1e9 + 7;
const ll     INF  = 1e18 + 100;
const int    inf  = 0x3f3f3f3f;
const double pi   = acos(-1.0);
const double eps  = 1e-8;
using namespace std;int n, m;
int cas, tol, T;char s[1010];
struct AC {int node[maxn][2], fail[maxn], cnt[maxn], vir[maxn];int root, sz;int newnode() {mes(node[++sz], 0);cnt[sz] = vir[sz] = 0;return sz;}void init() {sz = 0;root = newnode();}void insert(char *s, int f, int id) {int len = strlen(s+1);int rt = root;for(int i=1; i<=len; i++) {int k = s[i]-'0';if(node[rt][k] == 0) {node[rt][k] = newnode();}rt = node[rt][k];}if(f == 1)  cnt[rt] |= (1<<(id-1));else    vir[rt] = 1;}void build() {queue<int> q;while(!q.empty())   q.pop();fail[root] = root;for(int i=0; i<=1; i++) {if(node[root][i] == 0) {node[root][i] = root;} else {fail[node[root][i]] = root;q.push(node[root][i]);}}while(!q.empty()) {int u = q.front();q.pop();vir[u] |= vir[fail[u]];cnt[u] |= cnt[fail[u]];for(int i=0; i<=1; i++) {if(node[u][i] == 0) {node[u][i] = node[fail[u]][i];} else {fail[node[u][i]] = node[fail[u]][i];q.push(node[u][i]);}}}}int dis[maxn], point[500];bool vis[maxn];int cc[500][500], dp[500][1030];void bfs(int st) {queue<int> q;while(!q.empty())   q.pop();for(int i=1; i<=sz; i++)    dis[i] = inf;mes(vis, 0);q.push(point[st]);dis[point[st]] = 0;vis[point[st]] = 1;while(!q.empty()) {int u = q.front();q.pop();for(int i=0; i<=1; i++) {int k = node[u][i];if(vir[k])  continue;if(vis[k])  continue;dis[k] = dis[u]+1;vis[k] = true;q.push(k);}}for(int i=1; i<=tol; i++) {cc[st][i] = dis[point[i]];}}void handle() {tol = 0;point[++tol] = 1;for(int i=1; i<=sz; i++) {if(cnt[i]) {point[++tol] = i;}}for(int i=1; i<=tol; i++) {bfs(i);}//     for(int i=1; i<=tol; i++) {//         for(int j=1; j<=tol; j++) {//             printf("%d%c", cc[i][j], j==tol ? '\n' : ' ');//         }//     }//     for(int i=1; i<=tol; i++) {//         printf("cnt[%d] = %d\n", i, cnt[point[i]]);//     }}int solve() {int mx = (1<<n)-1;for(int i=1; i<=tol; i++) {for(int j=0; j<=mx; j++) {dp[i][j] = inf;}}dp[1][0] = 0;for(int j=0; j<=mx; j++) {for(int i=1; i<=tol; i++) {if(dp[i][j] == inf) continue;// printf("dp[%d][%d] = %d\n", i, j, dp[i][j]);for(int k=1; k<=tol; k++) {if(i == k)  continue;dp[k][j|cnt[point[k]]] = min(dp[k][j|cnt[point[k]]], dp[i][j]+cc[i][k]);}}}int ans = inf;for(int i=1; i<=tol; i++) {ans = min(ans, dp[i][mx]);}return ans;}
} ac;int main() {while(scanf("%d%d", &n, &m), n||m) {ac.init();for(int i=1; i<=n; i++) {scanf("%s", s+1);ac.insert(s, 1, i);}for(int i=1; i<=m; i++) {scanf("%s", s+1);ac.insert(s, 2, i);}ac.build();ac.handle();int ans = ac.solve();printf("%d\n", ans);}return 0;
}

轉載于:https://www.cnblogs.com/Jiaaaaaaaqi/p/10789881.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/393978.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/393978.shtml
英文地址,請注明出處:http://en.pswp.cn/news/393978.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

space index.php 7-14,disk_free_space()

disk_free_space()(PHP 4 > 4.1.0, PHP 5, PHP 7)返回目錄中的可用空間說明disk_free_space(string$directory):float給出一個包含有一個目錄的字符串&#xff0c;本函數將根據相應的文件系統或磁盤分區返回可用的字節數。參數$directory文件系統目錄或者磁盤分區。Note:如果…

云專網和云專線的區別_企業更適合互聯網專線還是云專線聯網?

隨著云計算、移動應用及全球化的發展&#xff0c;縱橫企業專網20年的MPLS專線弊端逐漸暴露&#xff0c;MPLS專線越來越難以滿足企業的業務發展需求&#xff0c;而云計算、SaaS及移動應用具有天然的互聯網屬性。為什么“互聯網”可以取代專線?互聯網的持續發展&#xff0c;為取…

composer安裝thinkphp

https://getcomposer.org/Composer-Setup.exe 正常安裝composer以后,執行composer create-project topthink/thinkphp myapp安裝thinkphp.轉載于:https://www.cnblogs.com/lijurui/p/6362012.html

wordpress 插件_如何為您的Web應用程序創建WordPress插件

wordpress 插件by Feedier by Alkalab由Feedier通過Alkalab 如何為您的Web應用程序創建WordPress插件 (How to create a WordPress plugin for your web app) Today, we are going to see how to create a very simple WordPress plugin for any web app that needs to insert…

Android 軟鍵盤相關問題

1. windowSoftInputMode屬性的使用 Android使用windowSoftInputMode來控制Activity 的主窗口與包含屏幕軟鍵盤的窗口的交互方式。 該屬性的設置影響兩個方面&#xff1a; 當 Activity 成為用戶注意的焦點時軟鍵盤的狀態 — 隱藏還是可見。對 Activity 主窗口所做的調整 — 是否…

git php框架,如何用Git安裝TP框架

本篇文章主要給大家介紹如何用Git安裝Thinkphp框架。關于TP框架的安裝&#xff0c;想必大家都知道較為常見的方式是通過composer安裝tp框架。首先簡單的給大家介紹下Git和TP框架。Git是一個開源的分布式版本控制系統&#xff0c;可以快速&#xff0c;高效地處理從小型到大型項目…

C#EF中,使用類似于SQL中的% 模糊查詢

最近在做項目的時候需要使用到模糊查詢,但是后臺使用EF寫的 而不是ADO或者是Dapper,如果是這樣的話,我們就可以使用Sql語句直接進行模糊查詢 現在我們需要在LINQ中使用類似于模糊查詢 在EF中有兩個方法:StartsWith()和EndWith() StartsWith(): 在轉到定義時 我們可以看見,這個方…

android toast居中顯示_Android Toast 設置到屏幕中間,自定義Toast的實現方法,及其說明...

Android Toast用于在手機屏幕上向用戶顯示一條信息&#xff0c;一段時間后信息會自動消失。信息可以是簡單的文本&#xff0c;也可以是復雜的圖片及其他內容(顯示一個view)。1.簡單用法Toast.makeText(midlet.getApplicationContext(), "用戶名不能為空", Toast.LENG…

leetcode103. 二叉樹的鋸齒形層次遍歷(bfs)

給定一個二叉樹&#xff0c;返回其節點值的鋸齒形層次遍歷。&#xff08;即先從左往右&#xff0c;再從右往左進行下一層遍歷&#xff0c;以此類推&#xff0c;層與層之間交替進行&#xff09;。例如&#xff1a; 給定二叉樹 [3,9,20,null,null,15,7],3/ \9 20/ \15 7 返回…

LintCode Find the Weak Connected Component in the Directed Graph

原題鏈接在這里&#xff1a;http://www.lintcode.com/en/problem/find-the-weak-connected-component-in-the-directed-graph/ 題目&#xff1a; Find the number Weak Connected Component in the directed graph. Each node in the graph contains a label and a list of its…

簡單了解tengine

Tengine是由淘寶網發起的Web服務器項目。它在Nginx的基礎上&#xff0c;針對大訪問量網站的需求&#xff0c;添加了很多高級功能和特性。最終目標是打造一個高效、穩定、安全、易用的Web平臺。1、基本的HTTP服務器特性1.處理靜態文件&#xff0c;索引文件以及自動索引&#xff…

服務器創建多個dhcp服務_如何在15分鐘內創建無服務器服務

服務器創建多個dhcp服務by Charlee Li通過李李 如何在15分鐘內創建無服務器服務 (How to create a serverless service in 15 minutes) The word “serverless” has been popular for quite a while. When Amazon released the AWS Lambda service in 2015, many tools emerg…

php snoopy視頻教程,php的Snoopy類

用了兩天這個類&#xff0c;發現很好用。獲取請求網頁里面的所有鏈接&#xff0c;直接使用fetchlinks就可以&#xff0c;獲取所有文本信息使用fetchtext(其內部還是使用正則表達式在進行處理)&#xff0c;還有其它較多的功能&#xff0c;如模擬提交表單等。使用方法&#xff1a…

網頁解析 css

網頁解析 css轉載于:https://www.cnblogs.com/guozepingboke/p/10792298.html

如何看pg數據庫版本號_查看pg數據庫版本

PostgreSQL 基本命令鏈接&#xff1a;http://blog.itpub.net/28602568/viewspace-1841163/標題&#xff1a;PostgreSQL 基本命令作者&#xff1a;&#xff4c;ōττ&#xff52;&#xff59;©版權所有[文章允許轉載,但必須以鏈接方式注明源地址,否則追究法律責任.]安裝步…

leetcode1091. 二進制矩陣中的最短路徑(bfs)

在一個 N N 的方形網格中&#xff0c;每個單元格有兩種狀態&#xff1a;空&#xff08;0&#xff09;或者阻塞&#xff08;1&#xff09;。一條從左上角到右下角、長度為 k 的暢通路徑&#xff0c;由滿足下述條件的單元格 C_1, C_2, ..., C_k 組成&#xff1a;相鄰單元格 C_i …

lock和synchronized的同步區別與選擇

區別如下&#xff1a; 1. lock是一個接口&#xff0c;而synchronized是java的一個關鍵字&#xff0c;synchronized是內置的語言實現&#xff1b;&#xff08;具體實現上的區別在《Java虛擬機》中有講解底層的CAS不同&#xff0c;以前有讀過現在又遺忘了。&#xff09; 2. syn…

首頁顯示登陸用戶名php,首頁登錄后怎么在首頁顯示用戶名以及隱藏登錄框?

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓index.php&#xff1a;登錄頁面用戶名&#xff1a;密碼&#xff1a;沒有賬號&#xff1f;立即注冊——————————————————————————doaction.php&#xff1a;header("Content-type:text/html;charsetutf…

react中使用構建緩存_通過在React中構建Tic Tac Toe來學習ReasonML

react中使用構建緩存3. 7. 2018: UPDATED to ReasonReact v0.4.23. 7. 2018&#xff1a;更新為ReasonReact v0.4.2 You may have heard of Reason before. It’s a syntax on top of OCaml that compiles to both readable JavaScript code and to native and bytecode as well…

echart vue 圖表大小_vue里echarts自適應窗口大小改變

echarts的圖表提供了一個resize方法可以自適應屏幕窗口改變&#xff0c;而重新渲染圖表大小的功能。因此我們只要監聽瀏覽器的窗口改變的resize事件&#xff0c;再結合echarts的圖表&#xff0c;就可以實現我們想要的功能了。如果是單個圖表的情況的話用window.onresize myCha…