刷题记录2015.11.17

原创 2015年11月17日 23:29:19

前两天家里电脑爆炸了,忙着装软件。

HDU2222:AC自动机裸题

HDU2896:同上

其实我还是没太懂自动机到底啥意思,感觉AC自动机和KMP没什么区别,不过KMP貌似也是一种自动机,但是在我看感觉所有程序都是自动机啊,求神犇解释一下。


代码如下:

HDU2222:

#include "cstdio"
#include "cstdlib"
#include "iostream"
#include "algorithm"
#include "cstring"
#include "queue"

using namespace std;

#define MAX_SIZE 10005
#define INF 0x3F3F3F3F
#define Eps
#define Mod

inline int Get_Int()
{
    int Num = 0, Flag = 1;
    char ch;
    do
    {
        ch = getchar();
        if(ch == '-')
            Flag *= -1;
    }
    while(ch < '0' || ch > '9');
    do
    {
        Num = Num * 10 + ch - '0';
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9');
    return Num * Flag;
}

class Node
{
public:
    Node *Next[26], *Fail;
    int Count;
}*Root, *Empty;

int Length[MAX_SIZE];
int M, N, T, Ans;

char Words[MAX_SIZE][55], Str[1000005];

inline Node* New()
{
    Node *x = (Node*)(malloc(sizeof(Node)));
    memset(x, 0, sizeof(Node));
    return x;
}

inline void Build_Trie()
{
    Root = New();
    for(int i = 1; i <= N; ++i)
    {
        Node *temp = Root;
        for(int j = 0; j < Length[i]; ++j)
        {
            int To = Words[i][j];
            if(!temp -> Next[To])
                temp -> Next[To] = New();
            temp = temp -> Next[To];
        }
        temp -> Count++;
    }
}

inline void Build_ACAM()
{
    queue<Node*> Queue;
    Queue.push(Root);
    Empty = New();
    for(int i = 0; i < 26; ++i)
        Empty -> Next[i] = Root;
    Root -> Fail = Empty;
    while(!Queue.empty())
    {
        Node *Now = Queue.front(), *temp;
        Queue.pop();
        for(int i = 0; i < 26; ++i)
            if(Now -> Next[i])
            {
                for(temp = Now -> Fail; !temp -> Next[i]; temp = temp -> Fail);
                Now -> Next[i] -> Fail = temp -> Next[i];
                Queue.push(Now -> Next[i]);
            }
    }
}

inline void Solve()
{
    Node *Now = Root;
    Ans = 0;
    for(int i = 0; i < M; ++i)
    {
        int To = Str[i];
        while(!Now -> Next[To])
            Now = Now -> Fail;
        Now = Now -> Next[To];
        for(Node* temp = Now; temp != Root; temp = temp -> Fail)
        {
            Ans += temp -> Count;
            temp -> Count = 0;
        }
    }
}

int main()
{
    cin >> T;
    while(T--)
    {
        N = Get_Int();
        for(int i = 1; i <= N; ++i)
        {
            scanf("%s", Words[i]);
            Length[i] = strlen(Words[i]);
            for(int j = 0; j < Length[i]; ++j)
                Words[i][j] -= 'a';
        }
        Build_Trie();
        Build_ACAM();
        scanf("%s", Str);
        M = strlen(Str);
        for(int i = 0; i < M; ++i)
            Str[i] -= 'a';
        Solve();
        printf("%d\n", Ans);
    }
    return 0;
}

HDU2896:

#include "cstdio"
#include "cstdlib"
#include "iostream"
#include "algorithm"
#include "cstring"
#include "queue"

using namespace std;

#define MAX_SIZE 
#define INF 0x3F3F3F3F
#define Eps
#define Mod

inline int Get_Int()
{
    int Num = 0, Flag = 1;
    char ch;
    do
    {
        ch = getchar();
        if(ch == '-')
            Flag *= -1;
    }
    while(ch < '0' || ch > '9');
    do
    {
        Num = Num * 10 + ch - '0';
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9');
    return Num * Flag;
}

class Node
{
public:
    Node *Fail, *Next[128];
    int Count;
}*Root, *Empty;

int N, M, Ans, Total;
int DATA[5];

char Virus[205], Web[10005];

inline Node* New()
{
    Node *x = (Node*)(malloc(sizeof(Node)));
    memset(x, 0, sizeof(Node));
    return x;
}

inline void Build_Trie()
{
    cin >> N;
    Root = New();
    for(int i = 1; i <= N; ++i)
    {
        scanf("%s", Virus);
        Node *Now = Root;
        for(int j = 0, Length = strlen(Virus), To; j < Length; ++j, Now = Now -> Next[To])
        {
            To = Virus[j];
            if(!Now -> Next[To])
                Now -> Next[To] = New();
        }
        Now -> Count = i;
    }
}

inline void Build_ACAM()
{
    queue<Node*> Queue;
    Queue.push(Root);
    Empty = New();
    for(int i = 0; i < 128; ++i)
        Empty -> Next[i] = Root;
    Root -> Fail = Empty;
    while(!Queue.empty())
    {
        Node *Now = Queue.front(), *temp;
        Queue.pop();
        for(int i = 0; i < 128; ++i)
            if(Now -> Next[i])
            {
                for(temp = Now -> Fail; !temp -> Next[i]; temp = temp -> Fail);
                Now -> Next[i] -> Fail = temp -> Next[i];
                Queue.push(Now -> Next[i]);
            }
    }
}

inline void Solve()
{
    cin >> M;
    for(int i = 1; i <= M; ++i)
    {
        Total = 0;
        scanf("%s", Web);
        Node *Now = Root;
        for(int j = 0, Length = strlen(Web); j < Length; ++j)
        {
            int To = Web[j];
            while(!Now -> Next[To])
                Now = Now -> Fail;
            Now = Now -> Next[To];
            for(Node* temp = Now; temp != Root; temp = temp -> Fail)
            {
                if(temp -> Count)
                    DATA[++Total] = temp -> Count;
                temp -> Count = 0;
            }
        }
        if(Total)
        {
            ++Ans;
            sort(DATA + 1, DATA + Total + 1);
            printf("web %d:", i);
            for(int i = 1; i <= Total; ++i)
                printf(" %d", DATA[i]);
            printf("\n");
        }
    }
    printf("total: %d\n", Ans);
}

int main()
{
    Build_Trie();
    Build_ACAM();
    Solve();
    return 0;
}


版权声明:都是TATQAQ2333大爷教我的

相关文章推荐

2015.11.17早6:50 黄河岸边,风景独好

美丽的风景,需要用心去发现。

近期总结-2015.11.17

近期总结 最近觉得还是把工作中的事情稍微记录一点,毕竟已经本科毕业好几个月了,也该记录点啥,凭着自娱自乐的精神还可以给以后的自己增加点小料。或许一年之后会想,这loser,当时怎么这么挫。。。 俗...

[R]2015.11.17 Practice

1. Commonly used function 1.1 options() Invoking options() with no arguments returns a list with t...

# 刷题记录——9.29

poj2079(贪心) poj1700(贪心)  1. poj2079     题意理解挺久的,在纠结needed colors是什么情况,后来看网上人说的才知道,是用来规定下限的……从...

CF贪心刷题记录

codeforces贪心刷题记录

【LeetCode刷题记录】Two Sum

题目Given an array of integers, find two numbers such that they add up to a specific target number.The...

vijos刷题记录(可能弃坑

突发奇想的记录下vijos的刷题情况(逃三月中旬(不知道什么时候) 突破100 rp 1500++ rank 900++四月十日 突破200 rp4005.19 rank 192. . ....

2-sat建图以及刷题记录~~

A[x] NOT A[x] A[x] AND A[y] A[x] AND NOT A[y] A[x] OR A[y] A[x] OR NOT A[y] NOT (A[x] ...

【LeetCode刷题记录】Rotate Array

LeetCode数组题之Rotate Array
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)