codeforces 918 B. Radio Station+AC自动机

http://codeforces.com/contest/918/problem/B
B. Radio Station
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
As the guys fried the radio station facilities, the school principal gave them tasks as a punishment. Dustin’s task was to add comments to nginx configuration for school’s website. The school has n servers. Each server has a name and an ip (names aren’t necessarily unique, but ips are). Dustin knows the ip and name of each server. For simplicity, we’ll assume that an nginx command is of form “command ip;” where command is a string consisting of English lowercase letter only, and ip is the ip of one of school servers.

Each ip is of form “a.b.c.d” where a, b, c and d are non-negative integers less than or equal to 255 (with no leading zeros). The nginx configuration file Dustin has to add comments to has m commands. Nobody ever memorizes the ips of servers, so to understand the configuration better, Dustin has to comment the name of server that the ip belongs to at the end of each line (after each command). More formally, if a line is “command ip;” Dustin has to replace it with “command ip; #name” where name is the name of the server with ip equal to ip.

Dustin doesn’t know anything about nginx, so he panicked again and his friends asked you to do his task for him.

Input
The first line of input contains two integers n and m (1 ≤ n, m ≤ 1000).

The next n lines contain the names and ips of the servers. Each line contains a string name, name of the server and a string ip, ip of the server, separated by space (1 ≤ |name| ≤ 10, name only consists of English lowercase letters). It is guaranteed that all ip are distinct.

The next m lines contain the commands in the configuration file. Each line is of form “command ip;” (1 ≤ |command| ≤ 10, command only consists of English lowercase letters). It is guaranteed that ip belongs to one of the n school servers.

Output
Print m lines, the commands in the configuration file after Dustin did his task.

Examples
input
2 2
main 192.168.0.2
replica 192.168.0.1
block 192.168.0.1;
proxy 192.168.0.2;
output
block 192.168.0.1; #replica
proxy 192.168.0.2; #main
input
3 5
google 8.8.8.8
codeforces 212.193.33.27
server 138.197.64.57
redirect 138.197.64.57;
block 8.8.8.8;
cf 212.193.33.27;
unblock 8.8.8.8;
check 138.197.64.57;
output
redirect 138.197.64.57; #server
block 8.8.8.8; #google
cf 212.193.33.27; #codeforces
unblock 8.8.8.8; #google
check 138.197.64.57; #server

这个题是我在学完AC自动机后遇到的第一个AC自动机的题,从思路到修修改改用了一个多小时,2个小时的codeforces比赛做完这个题只剩18分钟了,但是最后看到通过的那瞬间,满足感啊!!虽然还很菜,但是,只要努力,就有希望。一遍过,开心。

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
#define son_num 12
#define maxn 2000005
char str[1001][15];
char s[1001][15];
char web[1001][15];
char w[1001][15];
struct node
{
    int code;
    int terminal;
    node *fail;
    node *next[son_num];
    node()
    {
        fail=NULL;
        code=0;
        terminal=0;
        memset(next,NULL,sizeof(next));
    }
};
int bns[1005];
void insert(node *root,char *str,int x)
{
    node *p=root;
    int i=0,index;
    while(str[i])
    {
        if(str[i]=='.')
        {
            index=11;
        }
        else if(str[i]-'0'>=0&&str[i]-'0'<=9)
        {
            index=str[i]-'0';
        }
        else
        {
            index=12;
        }
        if(p->next[index]==NULL)
            p->next[index]=new node();
        p=p->next[index];
        i++;
    }
    p->code=x;
    p->terminal++;
}

void build_fail(node *root)
{
    queue<node*>que;
    root->fail=NULL;
    que.push(root);
    while(!que.empty())
    {
        node *temp=que.front();
        que.pop();
        node *p=NULL;
        for(int i=0; i<son_num; i++)
        {
            if(temp->next[i]!=NULL)
            {
                if(temp==root)  temp->next[i]->fail=root;
                else
                {
                    p=temp->fail;
                    while(p!=NULL)
                    {
                        if(p->next[i]!=NULL)
                        {
                            temp->next[i]->fail=p->next[i];
                            break;
                        }
                        p=p->fail;
                    }
                    if(p==NULL)
                        temp->next[i]->fail=root;
                }
                que.push(temp->next[i]);
            }
        }
    }
}
void query(node *root,char *str)
{
    int i=0,cnt=0,index,len,num=0;
    len=strlen(str);
    node *p=root;
    while(str[i])
    {
        if(str[i]=='.')
        {
            index=11;
        }
        else if(str[i]-'0'>=0&&str[i]-'0'<=9)
        {
            index=str[i]-'0';
        }
        else
        {
            index=12;
        }

        while(p->next[index]==NULL&&p!=root)
            p=p->fail;
        p=p->next[index];
        if(p==NULL)  p=root;
        node *temp=p;
        while(temp!=root&&temp->code)
        {
            bns[temp->code]=temp->terminal;
            temp=temp->fail;
        }
        i++;
    }
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        memset(bns,0,sizeof(bns));
        node *root=new node();
        for(int i=1; i<=n; i++)
        {
            scanf("%s%s",s[i],str[i]);
            insert(root,str[i],i);
        }
        build_fail(root);
        for(int j=1; j<=m; j++)
        {
            scanf("%s%s",w[j],web[j]);
            memset(bns,0,sizeof(bns));
            int t=strlen(web[j]);
            web[j][t-1]='\0';
            query(root,web[j]);
            for(int i=1; i<=n; i++)
            {
                if(bns[i]!=0)
                {
                    printf("%s %s; #%s\n",w[j],web[j],s[i]);
                }
            }
        }
    }
}

啥也不说了,原来。。。。。没通过,而且用map就可以很快写出来,我不管,我要把这个题用ac自动机ac了!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值