【小结】AC自动机

DFA AC 自动机

  • 考虑以下单词: {she, he, her}
  • 我们先画出它Trie树的模样

  • 留个板子

/* **********************************************

  File Name: ac_automata.cpp

  Auther: zhengdongjian@tju.edu.cn

  Created Time: 2015年08月14日 星期五 08时41分23秒

*********************************************** */
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> P;
const double EPS = 1e-8;
const double PI = acos(-1.0);

const int MAX = 500007;
const int MAXD = 26; //26 alphas
struct Trie {
    /* 
     * nxt & end is used in trie
     * fail is for ac automata
     */
    int nxt[MAX][MAXD], fail[MAX], end[MAX];
    int root, L; //root node, length(the nodes has been malloc)[0, L]
    int newnode() {
        memset(nxt[L], -1, sizeof(int) * MAXD);
        end[L++] = 0;
        return L - 1;
    }
    void clear() {
        L = 0;
        root = newnode();
    }
    void insert(char* buf) {
        int len = strlen(buf);
        int now = root;
        for (int i = 0; i < len; ++i) {
            if (nxt[now][buf[i] - 'a'] == -1) {
                nxt[now][buf[i] - 'a'] = newnode();
            }
            now = nxt[now][buf[i] - 'a'];
        }
        ++end[now];
    }
    void build() {
        queue<int> Q;
        fail[root] = root;
        for (int i = 0; i < MAXD; ++i) {
            if (nxt[root][i] == -1) {
                nxt[root][i] = root;
            } else {
                fail[nxt[root][i]] = root;
                Q.push(nxt[root][i]);
            }
        }

        while (!Q.empty()) {
            int now = Q.front();
            Q.pop();

            for (int i = 0; i < MAXD; ++i) {
                if (nxt[now][i] == -1) {
                    nxt[now][i] = nxt[fail[now]][i];
                } else {
                    fail[nxt[now][i]] = nxt[fail[now]][i];
                    Q.push(nxt[now][i]);
                }
            }
        }
    }
    int query(char* buf, int len = -1) {
        if (len == -1) {
            len = strlen(buf);
        }
        int now = root;
        int res = 0;
        for (int i = 0; i < len; ++i) {
            now = nxt[now][buf[i] - 'a'];
            int tmp = now;
            while (tmp != root) {
                res += end[tmp];
                end[tmp] = 0; //不重复,若可重复此处不置0即可
                tmp = fail[tmp];
            }
        }
        return res;
    }
    void debug() {
        for (int i = 0; i < L; ++i) {
            printf("%d, %d, %d, [%d", i, fail[i], end[i], nxt[i][0]);
            for (int j = 1; j < MAXD; ++j) {
                printf(" %d", nxt[i][j]);
            }
            puts("]");
        }
    }
} ac;

const int MAXL = 64;
char str[MAXL];
char buf[MAX << 1];

int main() {
    int T;
    scanf(" %d", &T);
    while (T--) {
        int n;
        scanf(" %d", &n);
        ac.clear();
        for (int i = 0; i < n; ++i) {
            scanf(" %s", str);
            ac.insert(str);
        }
        ac.build();

        scanf(" %s", buf);
        printf("%d\n", ac.query(buf));
    }
    return 0;
}

几个简单的小题目

  • hdu2222,First attempt
/* **********************************************

  File Name: ac_automata.cpp => hdu2222

  Auther: zhengdongjian@tju.edu.cn

  Created Time: 2015年08月14日 星期五 08时41分23秒

*********************************************** */
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> P;
const double EPS = 1e-8;
const double PI = acos(-1.0);

const int MAX = 500007;
const int MAXD = 26; //26 alphas
struct Trie {
    /* 
     * nxt & end is used in trie
     * fail is for ac automata
     */
    int nxt[MAX][MAXD], fail[MAX], end[MAX];
    int root, L; //root node, length(the nodes has been malloc)[0, L]
    int newnode() {
        memset(nxt[L], -1, sizeof(int) * MAXD);
        end[L++] = 0;
        return L - 1;
    }
    void clear() {
        L = 0;
        root = newnode();
    }
    void insert(char* buf) {
        int len = strlen(buf);
        int now = root;
        for (int i = 0; i < len; ++i) {
            if (nxt[now][buf[i] - 'a'] == -1) {
                nxt[now][buf[i] - 'a'] = newnode();
            }
            now = nxt[now][buf[i] - 'a'];
        }
        ++end[now];
    }
    void build() {
        queue<int> Q;
        fail[root] = root;
        for (int i = 0; i < MAXD; ++i) {
            if (nxt[root][i] == -1) {
                nxt[root][i] = root;
            } else {
                fail[nxt[root][i]] = root;
                Q.push(nxt[root][i]);
            }
        }

        while (!Q.empty()) {
            int now = Q.front();
            Q.pop();

            for (int i = 0; i < MAXD; ++i) {
                if (nxt[now][i] == -1) {
                    nxt[now][i] = nxt[fail[now]][i];
                } else {
                    fail[nxt[now][i]] = nxt[fail[now]][i];
                    Q.push(nxt[now][i]);
                }
            }
        }
    }
    int query(char* buf, int len = -1) {
        if (len == -1) {
            len = strlen(buf);
        }
        int now = root;
        int res = 0;
        for (int i = 0; i < len; ++i) {
            now = nxt[now][buf[i] - 'a'];
            int tmp = now;
            while (tmp != root) {
                res += end[tmp];
                end[tmp] = 0; //不重复,若可重复此处不置0即可
                tmp = fail[tmp];
            }
        }
        return res;
    }
    void debug() {
        for (int i = 0; i < L; ++i) {
            printf("%d, %d, %d, [%d", i, fail[i], end[i], nxt[i][0]);
            for (int j = 1; j < MAXD; ++j) {
                printf(" %d", nxt[i][j]);
            }
            puts("]");
        }
    }
} ac;

const int MAXL = 64;
char str[MAXL];
char buf[MAX << 1];

int main() {
    int T;
    scanf(" %d", &T);
    while (T--) {
        int n;
        scanf(" %d", &n);
        ac.clear();
        for (int i = 0; i < n; ++i) {
            scanf(" %s", str);
            ac.insert(str);
        }
        ac.build();

        scanf(" %s", buf);
        printf("%d\n", ac.query(buf));
    }
    return 0;
}
  • hdu2896
  • 病毒保证不同,简单统计,随便搞
/* **********************************************

  File Name: 2896.cpp

  Auther: zhengdongjian@tju.edu.cn

  Created Time: 2015年08月14日 星期五 11时25分51秒

*********************************************** */
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> P;
const double EPS = 1e-8;
const double PI = acos(-1.0);
const int MAX = 100007;
const int MAXD = 128;
struct Trie {
    int nxt[MAX][MAXD], fail[MAX], end[MAX];
    int root, L;

    int newnode() {
        memset(nxt[L], -1, sizeof(int) * MAXD);
        end[L++] = -1;
        return L - 1;
    }

    void clear() {
        L = 0;
        root = newnode();
    }

    void insert(char* buf, int _end) {
        int len = strlen(buf);
        int now = root;
        for (int i = 0; i < len; ++i) {
            if (nxt[now][(int)buf[i]] == -1) {
                nxt[now][(int)buf[i]] = newnode();
            }
            now = nxt[now][(int)buf[i]];
        }
        end[now] = _end;
    }

    void build() {
        queue<int> Q;
        fail[root] = root;
        for (int i = 0; i < MAXD; ++i) {
            if (nxt[root][i] == -1) {
                nxt[root][i] = root;
            } else {
                fail[nxt[root][i]] = root;
                Q.push(nxt[root][i]);
            }
        }

        while (!Q.empty()) {
            int now = Q.front();
            Q.pop();

            for (int i = 0; i < MAXD; ++i) {
                if (nxt[now][i] == -1) {
                    nxt[now][i] = nxt[fail[now]][i];
                } else {
                    fail[nxt[now][i]] = nxt[fail[now]][i];
                    Q.push(nxt[now][i]);
                }
            }
        }
    }
    set<int> query(char* buf) {
        int len = strlen(buf);
        int now = root;
        set<int> res;
        for (int i = 0; i < len; ++i) {
            now = nxt[now][(int)buf[i]];
            int tmp = now;
            while (tmp != root) {
                if (~end[tmp]) {
                    res.insert(end[tmp]);
                }
                tmp = fail[tmp];
            }
        }
        return res;
    }
} ac;
char buf[MAX];

int main() {
    int n, m;
    while (~scanf(" %d", &n)) {
        ac.clear();
        for (int i = 1; i <= n; ++i) {
            scanf(" %s", buf);
            ac.insert(buf, i);
        }
        ac.build();
        scanf(" %d", &m);
        int sum = 0;
        for (int i = 1; i <= m; ++i) {
            scanf(" %s", buf);
            auto v = ac.query(buf);
            if (!v.empty()) {
                ++sum;
                printf("web %d:", i);
                for (auto it = v.begin(); it != v.end(); ++it) {
                    printf(" %d", *it);
                }
                puts("");
            }
        }
        printf("total: %d\n", sum);
    }
    return 0;
}
  • hdu3065
  • 要打印匹配串:打标记,Trie上节点打前驱和字符标记。P.S.空间上还可以优化
/* **********************************************

  File Name: 3065.cpp

  Auther: zhengdongjian@tju.edu.cn

  Created Time: 2015年08月14日 星期五 11时46分10秒

*********************************************** */
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> P;
const double EPS = 1e-8;
const double PI = acos(-1.0);

const int MAX = 50007;
const int MAXD = 128; //26 alphas
struct Trie {
    /* 
     * nxt & end is used in trie
     * fail is for ac automata
     */
    int nxt[MAX][MAXD], fail[MAX], end[MAX], pre[MAX];
    char dad[MAX];
    int root, L; //root node, length(the nodes has been malloc)[0, L]
    int newnode() {
        memset(nxt[L], -1, sizeof(int) * MAXD);
        pre[L] = -1;
        end[L++] = 0;
        return L - 1;
    }
    void clear() {
        L = 0;
        root = newnode();
    }
    void insert(char* buf) {
        int len = strlen(buf);
        int now = root;
        for (int i = 0; i < len; ++i) {
            if (nxt[now][(int)buf[i]] == -1) {
                nxt[now][(int)buf[i]] = newnode();
                pre[nxt[now][(int)buf[i]]] = now;
                dad[nxt[now][(int)buf[i]]] = buf[i];
            }
            now = nxt[now][(int)buf[i]];
        }
        ++end[now];
    }

    void build() {
        queue<int> Q;
        fail[root] = root;
        for (int i = 0; i < MAXD; ++i) {
            if (nxt[root][i] == -1) {
                nxt[root][i] = root;
            } else {
                fail[nxt[root][i]] = root;
                Q.push(nxt[root][i]);
            }
        }

        while (!Q.empty()) {
            int now = Q.front();
            Q.pop();

            for (int i = 0; i < MAXD; ++i) {
                if (nxt[now][i] == -1) {
                    nxt[now][i] = nxt[fail[now]][i];
                } else {
                    fail[nxt[now][i]] = nxt[fail[now]][i];
                    Q.push(nxt[now][i]);
                }
            }
        }
    }
    map<int, int> query(char* buf, int len = -1) {
        if (len == -1) {
            len = strlen(buf);
        }
        int now = root;
        map<int, int> res;
        for (int i = 0; i < len; ++i) {
            now = nxt[now][(int)buf[i]];
            int tmp = now;
            while (tmp != root) {
                if (end[tmp] > 0) {
                    ++res[tmp];
                }
                tmp = fail[tmp];
            }
        }
        return res;
    }
    void debug() {
        for (int i = 0; i < L; ++i) {
            printf("%d, %d, %d, [%d", i, fail[i], end[i], nxt[i][0]);
            for (int j = 1; j < MAXD; ++j) {
                printf(" %d", nxt[i][j]);
            }
            puts("]");
        }
    }
} ac;

const int MAXL = 64;
char str[MAXL];
char buffer[MAX * 40];

int main() {
    int n;
    while (~scanf(" %d", &n)) {
        ac.clear();
        for (int i = 1; i <= n; ++i) {
            scanf(" %s", str);
            ac.insert(str);
        }
        ac.build();
        scanf(" %s", buffer);
        auto mp = ac.query(buffer);
        for (auto it = mp.begin(); it != mp.end(); ++it) {
            int now = it->first;
            int idx = MAXL - 1;
            str[idx--] = '\0';
            while (now != ac.root) {
                str[idx--] = ac.dad[now];
                now = ac.pre[now];
            }
            ++idx;
            printf("%s: %d\n", str + idx, it->second);
        }
    }
    return 0;
}
  • zoj3430
  • 解码一下即可。debug了好久,最后发现Base64直接解码出来的字符可能不是 ASCII 码,就如 1111 1111 对应到 ASCII 后是 EOF 真让人难堪。。。所以字符集开到256就可以过了…一部分调试中间修改导致整个代码看起来丑陋了一些,不愿改了。。
/* **********************************************

  File Name: 3430.cpp

  Auther: zhengdongjian@tju.edu.cn

  Created Time: 2015年08月14日 星期五 13时28分38秒

*********************************************** */
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> P;
const double EPS = 1e-8;
const double PI = acos(-1.0);
const int MAX = 50007;
const int MAXD = 256;

/*
 * Base64 Decode
 */
static const char cb64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
bool pool[50007];
inline int dic64(char& c) {
    if (isupper(c)) {
        return c - 'A';
    } else if (islower(c)) {
        return c - 'a' + 26;
    } else if (isdigit(c)) {
        return c - '0' + 52;
    } else {
        return c == '+' ? 62 : 63;
    }
}
void decode64(char source[], int dest[]) {
    int len = strlen(source);
    int bit = len * 6;
    memset(pool, false, sizeof(bool) * bit);
    while (source[len - 1] == '=') {
        --len;
        bit -= 8;
    }
    //printf("bit = %d\n", bit);
    for (int i = 0, j = 0; i < len; ++i, j += 6) {
        int c = dic64(source[i]);
        for (int k = j + 5; k >= j; --k) {
            pool[k] = c & 1;
            c >>= 1;
        }
    }
    int p = 0;
    for (int i = 0; i < bit; i += 8) {
        dest[p] = 0;
        for (int j = 0; j < 8; ++j) {
            dest[p] <<= 1;
            if (pool[i + j]) {
                ++dest[p];
                //dest[p] = (char)((int)dest[p] + 1);
            }
        }
        ++p;
    }
    dest[p] = -1;
}
/**********************************************/

struct Trie {
    /* 
     * nxt & end is used in trie
     * fail is for ac automata
     */
    int nxt[MAX][MAXD], fail[MAX], end[MAX];
    int root, L; //root node, length(the nodes has been malloc)[0, L]
    int newnode() {
        memset(nxt[L], -1, sizeof(int) * MAXD);
        end[L++] = 0;
        return L - 1;
    }
    void clear() {
        L = 0;
        root = newnode();
    }
    void insert(int* buf, int _id) {
        int* p = buf;
        int now = root;
        while (~(*p)) {
            if (nxt[now][*p] == -1) {
                nxt[now][*p] = newnode();
            }
            now = nxt[now][*p++];
        }
        end[now] = _id;
    }
    void build() {
        queue<int> Q;
        fail[root] = root;
        for (int i = 0; i < MAXD; ++i) {
            if (nxt[root][i] == -1) {
                nxt[root][i] = root;
            } else {
                fail[nxt[root][i]] = root;
                Q.push(nxt[root][i]);
            }
        }

        while (!Q.empty()) {
            int now = Q.front();
            Q.pop();

            for (int i = 0; i < MAXD; ++i) {
                if (nxt[now][i] == -1) {
                    nxt[now][i] = nxt[fail[now]][i];
                } else {
                    fail[nxt[now][i]] = nxt[fail[now]][i];
                    Q.push(nxt[now][i]);
                }
            }
        }
    }
    set<int> query(int* buf) {
        int* p = buf;
        int now = root;
        set<int> res;
        while (~(*p)) {
            now = nxt[now][*p++];
            int tmp = now;
            while (tmp != root) {
                if (end[tmp]) {
                    res.insert(end[tmp]);
                }
                tmp = fail[tmp];
            }
        }
        return res;
    }
    void debug() {
        for (int i = 0; i < L; ++i) {
            printf("%d, %d, %d, [%d", i, fail[i], end[i], nxt[i][0]);
            for (int j = 1; j < MAXD; ++j) {
                printf(" %d", nxt[i][j]);
            }
            puts("]");
        }
    }
} ac;

const int MAXL = 128;
char str[MAXL];
char buf[MAX];
int jj[MAX];

int main() {
    /*
    while (cin >> buf) {
        decode64(buf, buf);
        cout << buf << endl;
    }
    return 0;
    */

    int n;
    while (~scanf(" %d", &n)) {
        ac.clear();
        for (int i = 1; i <= n; ++i) {
            scanf(" %s", str);
            decode64(str, jj);
            ac.insert(jj, i);
        }
        ac.build();

        int m;
        scanf(" %d", &m);
        while (m--) {
            scanf(" %s", buf);
            decode64(buf, jj);
            printf("%d\n", (int)ac.query(jj).size());
        }
        puts("");
    }
    return 0;
}
深度学习是机器学习的一个子领域,它基于人工神经网络的研究,特别是利用多层次的神经网络来进行学习和模式识别。深度学习模型能够学习数据的高层次特征,这些特征对于图像和语音识别、自然语言处理、医学图像分析等应用至关重要。以下是深度学习的一些关键概念和组成部分: 1. **神经网络(Neural Networks)**:深度学习的基础是人工神经网络,它是由多个层组成的网络结构,包括输入层、隐藏层和输出层。每个层由多个神经元组成,神经元之间通过权重连接。 2. **前馈神经网络(Feedforward Neural Networks)**:这是最常见的神经网络类型,信息从输入层流向隐藏层,最终到达输出层。 3. **卷积神经网络(Convolutional Neural Networks, CNNs)**:这种网络特别适合处理具有网格结构的数据,如图像。它们使用卷积层来提取图像的特征。 4. **循环神经网络(Recurrent Neural Networks, RNNs)**:这种网络能够处理序列数据,如时间序列或自然语言,因为它们具有记忆功能,能够捕捉数据中的时间依赖性。 5. **长短期记忆网络(Long Short-Term Memory, LSTM)**:LSTM 是一种特殊的 RNN,它能够学习长期依赖关系,非常适合复杂的序列预测任务。 6. **生成对抗网络(Generative Adversarial Networks, GANs)**:由两个网络组成,一个生成器和一个判别器,它们相互竞争,生成器生成数据,判别器评估数据的真实性。 7. **深度学习框架**:如 TensorFlow、Keras、PyTorch 等,这些框架提供了构建、训练和部署深度学习模型的工具和库。 8. **激活函数(Activation Functions)**:如 ReLU、Sigmoid、Tanh 等,它们在神经网络中用于添加非线性,使得网络能够学习复杂的函数。 9. **损失函数(Loss Functions)**:用于评估模型的预测与真实值之间的差异,常见的损失函数包括均方误差(MSE)、交叉熵(Cross-Entropy)等。 10. **优化算法(Optimization Algorithms)**:如梯度下降(Gradient Descent)、随机梯度下降(SGD)、Adam 等,用于更新网络权重,以最小化损失函数。 11. **正则化(Regularization)**:技术如 Dropout、L1/L2 正则化等,用于防止模型过拟合。 12. **迁移学习(Transfer Learning)**:利用在一个任务上训练好的模型来提高另一个相关任务的性能。 深度学习在许多领域都取得了显著的成就,但它也面临着一些挑战,如对大量数据的依赖、模型的解释性差、计算资源消耗大等。研究人员正在不断探索新的方法来解决这些问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值