【HDU5880】Family View(AC自动机)

记录一个菜逼的成长。。

题目链接

题目大意:

有n个只包含小写字母的字符串。给你一个字符串T。如果n个字符串里存在字符串在T里,就将这个字符串在T里标记为*。(忽略大小写)

笔记

算是比较裸的AC自动机。
由n个字符串构建AC自动机。
匹配一遍,记录起始位置,然后循环标记即可。

#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#include <utility>
#include <iostream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first 
#define se second
#define clr clear()
#define cl(a,b) memset(a,b,sizeof a)
const int maxn = 1000000 + 10;
typedef long long LL;
typedef pair<int,int> PII;
vector<PII>a;
struct Trie
{
    int next[1000010][26],fail[1000010],end[1000010],deep[maxn];
    int root,L;
    int newnode()
    {
        for(int i = 0;i < 26;i++)
            next[L][i] = -1;
        end[L++] = 0;
        return L-1;
    }
    void init()
    {
        L = 0;
        root = newnode();
    }
    void insert(char buf[])
    {
        int len = strlen(buf);
        int now = root;
        for(int i = 0;i < len;i++)
        {
            if(next[now][buf[i]-'a'] == -1)
                next[now][buf[i]-'a'] = newnode();
            now = next[now][buf[i]-'a'];
        }
        end[now]++;
        deep[now] = len;
    }
    void build()
    {
        queue<int>Q;
        fail[root] = root;
        for(int i = 0;i < 26;i++)
            if(next[root][i] == -1)
                next[root][i] = root;
            else
            {
                fail[next[root][i]] = root;
                Q.push(next[root][i]);
            }
        while( !Q.empty() )
        {
            int now = Q.front();
            Q.pop();
            for(int i = 0;i < 26;i++)
                if(next[now][i] == -1)
                    next[now][i] = next[fail[now]][i];
                else
                {
                    fail[next[now][i]]=next[fail[now]][i];
                    Q.push(next[now][i]);
                }
        }
    }
    int query(char buf[])
    {
        int len = strlen(buf);
        int now = root;
        int res = 0;
        for(int i = 0;i < len;i++)
        {
            now = next[now][buf[i]-'a'];
            int temp = now;
            while( temp != root )
            {
                res += end[temp];
                if (end[temp]) {
                  a.pb(mp(i-deep[temp],i));//重点
                }
                temp = fail[temp];
            }
        }
        return res;
    }
}ac;

char str[maxn];
bool vis[maxn];
int main() 
{
  int T;scanf("%d",&T);
  while(T--) {
    ac.init();a.clr;
    int n;scanf("%d\n",&n);
    for (int i = 1; i <= n; i++) {
      gets(str);
      ac.insert(str);
    }
    ac.build();
    gets(str);cl(vis,false);
    for (int i = 0; str[i]; i++) {
      if(isupper(str[i]))vis[i] = true;
      str[i] = tolower(str[i]);
    }
    ac.query(str);
    for (auto x : a) {
      for (int i = x.fi + 1; i <= x.se; i++)
        str[i] = '*';
    }
    for (int i = 0; str[i]; i++) {
      if(vis[i]) {
        if(str[i] != '*')putchar(toupper(str[i]));
        else putchar(str[i]);
      }
      else putchar(str[i]);
    }
    puts("");
  }
  return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值