`
阿尔萨斯
  • 浏览: 4176001 次
社区版块
存档分类
最新评论

uva 1076 - Password Suspects(AC自动机+记忆化搜索)

 
阅读更多

题目链接:uva 1076 - Password Suspects

题目大意:有一个长度为n的密码,存在m个子串,问说有多少种字符串满足,如果满足个数不大于42,按照字典序输出。

解题思路:根据子串构建AC自动机,然后记忆化搜索,dp[i][u][s]表示第i个字符,在u节点,匹配s个子串。

#include <cstdio>
#include <cstring>
#include <queue>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;

const int maxn = 105;
const int maxs = (1<<10)+5;
const int sigma_size = 26;

struct Aho_Corasick {
    int sz, g[maxn][sigma_size];
    int val[maxn], fail[maxn];

    void init();
    int idx(char ch);
    void insert(char* str, int k);
    void get_fail();
}AC;

int N, M;
vector<string> vec;
ll dp[30][maxn][maxs];

void init () {
    AC.init();
    char str[20];

    for (int i = 0; i < M; i++) {
        cin >> str;
        AC.insert(str, i);
    }
    AC.get_fail();
    memset(dp, -1, sizeof(dp));
}

ll solve (int d, int u, int s) {

    if (d >= N)
        return s == (1<<M)-1 ? 1 : 0;

    if (dp[d][u][s] != -1)
        return dp[d][u][s];

    ll& ret = dp[d][u][s];
    ret = 0;

    for (int i = 0; i < sigma_size; i++) {
        int v = AC.g[u][i];
        ret += solve(d + 1, v, s | AC.val[v]);
    }
    return ret;
}

void search (int d, int u, int s, string str) {

    if (d >= N) {
        if (s == (1<<M) - 1)
            vec.push_back(str);
        return;
    }

    if (dp[d][u][s] <= 0)
        return ;

    for (int i = 0; i < sigma_size; i++) {
        int v = AC.g[u][i];
        char ch = 'a' + i;
        search(d + 1, v, s | AC.val[v], str + ch);
    }
}

int main () {
    int cas = 0;
    while (scanf("%d%d", &N, &M) == 2 && N + M) {
        init();
        ll ans = solve(0, 0, 0);
        //printf("Case %d: %lld suspects\n", ++cas, ans);
        cout << "Case " << ++cas << ": " << ans << " suspects" << endl;

        if (ans <= 42) {
            vec.clear();
            search(0, 0, 0, "");
            sort(vec.begin(), vec.end());
            for (int i = 0; i < vec.size(); i++)
                cout << vec[i] << endl;
        }
    }
    return 0;
}

void Aho_Corasick::init() {
    sz = 1;
    memset(g[0], 0, sizeof(g[0]));
}

int Aho_Corasick::idx(char ch) {
    return ch - 'a';
}

void Aho_Corasick::insert(char* str, int k) {
    int u = 0, n = strlen(str);

    for (int i = 0; i < n; i++) {
        int v = idx(str[i]);
        if (g[u][v] == 0) {
            val[sz] = 0;
            memset(g[sz], 0, sizeof(g[sz]));
            g[u][v] = sz++;
        }
        u = g[u][v];
    }
    val[u] |= (1<<k);
}

void Aho_Corasick::get_fail() {
    queue<int> que;

    for (int i  = 0; i < sigma_size; i++) {
        int u = g[0][i];
        if (u) {
            fail[u] = 0;
            que.push(u);
        }
    }

    while (!que.empty()) {
        int r = que.front();
        que.pop();

        for (int i = 0; i < sigma_size; i++) {
            int u = g[r][i];

            if (u == 0) {
                g[r][i] = g[fail[r]][i];
                continue;
            }

            que.push(u);
            int v = fail[r];
            while (v && g[v][i] == 0)
                v = fail[v];

            fail[u] = g[v][i];
            val[u] |= val[fail[u]];
        }
    }
}
分享到:
评论

相关推荐

    phaser-the-usual-suspects:那些你看过一百万次的游戏,现在有了 Phaser

    ##The Usual Suspects 拼图、发现差异、拖拽匹配等,这些游戏真的很简单,但却是你最终一遍又一遍地做的那种游戏,游戏的常见嫌疑人。 这是一组蓝图,您可以下载源代码并制作自己的游戏。 这些蓝图或模板只是作为一...

    poj 1611 The Suspects.md

    poj 1611 The Suspects.md

    poj 1611 The Suspects 代码

    poj 1611 The Suspects 代码 并查集的应用

    unusual-suspects

    目录自动格式化代码更改页面&lt;title&gt; 安装依赖项导入组件代码分割添加样式表后处理CSS 添加CSS预处理器(Sass,Less等) 添加图像,字体和文件使用public文件夹更改HTML 在模块系统之外添加资产何时使用public...

    ACM ZJU 1789 Suspects

    用最简洁的语言实现了一下并查集,欢迎下载,如有意见或建议可以提出,谢谢大家的支持!:) #include using namespace std; int n,m,k; struct TreeNode { int parent;...void init() //并查集初始化

    MemoryAnalyzer-1.9.1.20190826-linux.gtk.x86_64.zip

    官网一直下载不了,找这个找了好久,因为生成的dump文件太大不得不用linux..../ParseHeapDump.sh java_pid1089.hprof org.eclipse.mat.api:suspects org.eclipse.mat.api:overview org.eclipse.mat.api:top_components

    ha457-2021-5-12.jar.zip

    Summary IBM HeapAnalyzer is a graphical tool ... a similar feature set (finding large dominators, leak suspects, etc.), and the IBM Extensions for Memory Analyzer with product-specific analysis engines.

    MemoryAnalyzer-1.2.0.20120530-win32.win32.x86_64.zip

    Memory Analyzer (MAT) The Eclipse Memory Analyzer is a fast and feature-rich Java heap analyzer that helps you find memory leaks and reduce memory ... run a report to automatically extract leak suspects.

    Web Security Testing Cookbook

    pinpointed at Ajax functions, as well as large multi-step tests for the usual suspects: cross-site scripting and injection attacks. This book helps you: Obtain, install, and configure useful-and free-...

    Android代码-DiscogsBrowser

    A direct link to the latest APK can be found here DiscogsBrowser Native Android app to interact with the Discogs' public API.... This app uses the usual suspects: Dagger2 Retrofit2 Rx

    MemoryAnalyzer-1.7.0.20170613-macosx.cocoa.x86_64.zip

    quickly calculate the retained sizes of objects, see who is preventing the Garbage Collector from collecting objects, run a report to automatically extract leak suspects. 检测内存泄漏的利器,协助...

    Web Security Testing Cookbook: Systematic Techniques to Find Problems Fast

    By the end of the book, you’ll be able to build tests pinpointed at Ajax functions, as well as large multi-step tests for the usual suspects: cross-site scripting and injection attacks. This book ...

    poj经典数据结构题目解题报告

    Net&lt;br&gt;Pku acm 3278 Catch That Cow&lt;br&gt;Pku acm 2253 Frogger&lt;br&gt;Pku acm 1062 昂贵的聘礼 Pku acm 1125 Stockbroker Grapevine Pku acm 1611 The Suspects Pku acm 2492 A Bug's Life 更多请访问:...

    A Methodological Model

    focus their activities, geographically prioritize suspects, and to concentrate saturation or directed patrolling efforts in those zones where the criminal predator is most likely to be active. By ...

    ml-glossary:机器学习词汇

    机器学习词汇 寻找其他维护者! 很抱歉我没有回应。 :(我去过Cruise公司,为ML infra提供自动驾驶汽车,而且还没有永远审查此回购协议。...# Assumes you have the usual suspects installed: numpy, sc

    ZOJ Problem Set – 2003 Substitution Cipher

    The ACM director suspects one of his competitors of reading his correspondece. To prevent other companies from revealing his secret, he decided to use a substitution cipher in all the letters ...

    Windows Forensics Cookbook

    Collect and validate evidence from suspects and computers and uncover clues that are otherwise difficult Packed with powerful recipes to perform highly effective field investigations Book Description...

    19春第三题 PAT甲级 1158 Telefraud Detection (25分) 用这个方法最好

    题目 Telefraud(电信诈骗) remains a ... To stop this crime, you are supposed to write a program to detect those suspects from a huge amount of phone call records. A person must be detected as a sus

Global site tag (gtag.js) - Google Analytics