HDU 5880 Family View

$AC$自动机。

用$AC$自动机匹配一次,开一个$flag$记录一下以$i$位置为结尾的最长要打$*$多少个字符,然后倒着扫描一次就可以输出了。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<bitset>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-6;
void File()
{
    freopen("D:\\in.txt","r",stdin);
    freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
    char c=getchar(); x=0;
    while(!isdigit(c)) c=getchar();
    while(isdigit(c)) {x=x*10+c-‘0‘; c=getchar();}
}

const int maxn=1000010;
int flag[maxn];
char buf[maxn];

struct Trie
{
    int next[maxn][26],fail[maxn],end[maxn],Len[maxn];
    int root,L;
    int newnode()
    {
        for(int i = 0;i < 26;i++) next[L][i] = -1;
        Len[L]= end[L] = 0; L++;
        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]++;
        Len[now]=strlen(buf);
    }
    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]);
                }
        }
    }
    void query(char buf[])
    {
        int len = strlen(buf);
        int now = root;
        int res = 0;
        for(int i = 0;i < len;i++)
        {
            int sign;
            if(buf[i]>=‘A‘&&buf[i]<=‘Z‘) sign=buf[i]-‘A‘;
            else if(buf[i]>=‘a‘&&buf[i]<=‘z‘) sign=buf[i]-‘a‘;
            else { now=root; continue; }
            now = next[now][sign];
            int temp = now;
            while( temp != root )
            {
                flag[i]=max(flag[i],Len[temp]);
                temp = fail[temp];
            }
        }
    }
};

Trie ac;

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

        getchar(); gets(buf); memset(flag,0,sizeof flag);
        ac.query(buf);

        int leng=strlen(buf); int num=0;

        for(int i=leng-1;i>=0;i--)
        {
            num=max(num,flag[i]);
            if(num==0) continue;
            else buf[i]=‘*‘, num--;
        }
        printf("%s\n",buf);
    }
    return 0;
}
时间: 2024-11-07 10:39:31

HDU 5880 Family View的相关文章

HDU 5880 Family View (AC自动机)

Family View Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Description Steam is a digital distribution platform developed by Valve Corporation offering digital rights management (DRM), multiplayer gaming and socia

HDU 5880 Family View (2016 青岛网络赛 C题,AC自动机)

题目链接  2016 青岛网络赛  Problem C 题意  给出一些敏感词,和一篇文章.现在要屏蔽这篇文章中所有出现过的敏感词,屏蔽掉的用$'*'$表示. 建立$AC$自动机,查询的时候沿着$fail$指针往下走,当匹配成功的时候更新$f[i]$ $f[i]$表示要屏蔽以第$i$个字母结尾的长度为$f[i]$的字符串. 原文地址:https://www.cnblogs.com/cxhscst2/p/8452147.html

hdu 5880 AC自动机

Family View Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 175    Accepted Submission(s): 20 Problem Description Steam is a digital distribution platform developed by Valve Corporation offering

HDU 4054 - Hexadecimal View

先上一枚水题,模拟. /* ID:esxgx1 LANG:C++ PROG:hdu4054 */ #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; int main(void) { #ifndef ONLINE_JUDGE freopen("in.txt", "r", st

HDU 4054 Hexadecimal View 模拟

戳这里:HDU 4054 //复习一下 cin.getline() 的用法 1 #include "bits/stdc++.h" 2 using namespace std; 3 char str[5000]; 4 5 char Change(char c) 6 { 7 if('A' <= c && c <= 'Z') { 8 return c + 32; 9 } 10 if('a' <= c && c <= 'z') { 11

AC自动机及KMP练习

好久都没敲过KMP和AC自动机了.以前只会敲个kuangbin牌板子套题.现在重新写了自己的板子加深了印象.并且刷了一些题来增加自己的理解. KMP网上教程很多,但我的建议还是先看AC自动机(Trie图)的构造后再去理解.板子的话大家大同小异. 而AC自动机的构造则是推荐王贇的<Trie图的构建.活用与改进>. 前面的备用知识则是字典树.推荐董华星的<浅析字母树在信息学竞赛中的应用>.董聚聚不仅仅是介绍了字典树,包括一些常见的应用也有论述,介绍的挺详细的. 接下来就是刷题的部分了.

好吧就让我们结束这一切吧

http://www.tudou.com/programs/view/K7lbU7LsiJI/HGN13.htmlhttp://www.tudou.com/programs/view/SfcF7r7DsCk/XCWU7.htmlhttp://www.tudou.com/programs/view/BbljqbN52ZY/ieGvM.htmlhttp://www.tudou.com/programs/view/R4thdEitDik/17gGr.htmlhttp://www.tudou.com/p

法涉法而尴尬的收入法国

http://www.tudou.com/programs/view/pQOO07vn4Sc/l99Fi.htmlhttp://www.tudou.com/programs/view/EDJkM6ojYkc/r7RA6.htmlhttp://www.tudou.com/programs/view/Gf0zVZygPtU/EmvXY.htmlhttp://www.tudou.com/programs/view/UhqFbgbyQ6k/Z23Lx.htmlhttp://www.tudou.com/p

我不要说谎好吗

http://www.tudou.com/programs/view/KhjSmQKHEzM/McEy9.htmlhttp://www.tudou.com/programs/view/5FNDTTRKi6Y/9719g.htmlhttp://www.tudou.com/programs/view/UWFf0dz2DEk/3xPj2.htmlhttp://www.tudou.com/programs/view/b2zsYr4dCZg/gv6FP.htmlhttp://www.tudou.com/p