zoukankan      html  css  js  c++  java
  • [POI2000]病毒

    [POI2000]病毒

    只要前面那道文本生成器会的话,这题应该很简单了。

    建树方法一模一样,甚至连求(ok)都一模一样。

    之后采取dfs爆搜答案。

    从根开始,只走(ok=true)的儿子,如果走出一个环,那就有合法串(把环的部分提取出来,在任意地方断环成链,再复制粘贴无数遍,就是一个合法的无限长度的串)。否则,则没有合法串。

    这是dfs的代码:

    inline void dfs(int x){
    	if(vis[x]){puts("TAK");exit(0);}
    	vis[x]=true;
    	for(register int i=0;i<2;i++)if(t[t[x].ch[i]].ok)dfs(t[x].ch[i]);
    	vis[x]=false;
    }
    

    然后这是总代码:

    #include<bits/stdc++.h>
    using namespace std;
    int n,S,cnt=1;
    char s[30100];
    bool vis[30100];
    struct AC_Automaton{
    	int ch[2],fail;
    	bool ok;
    }t[30100];
    inline void ins(){
    	register int x=1;
    	for(register int i=0;i<S;i++){
    		if(!t[x].ch[s[i]-'0'])t[x].ch[s[i]-'0']=++cnt,t[cnt].ok=true;
    		x=t[x].ch[s[i]-'0'];
    	}
    	t[x].ok=false;
    }
    queue<int>q;
    inline void build(){
    	for(register int i=0;i<2;i++){
    		if(t[1].ch[i])t[t[1].ch[i]].fail=1,q.push(t[1].ch[i]);
    		else t[1].ch[i]=1;
    	}
    	while(!q.empty()){
    		register int x=q.front();q.pop();
    		for(register int i=0;i<2;i++){
    			if(t[x].ch[i])t[t[x].ch[i]].ok&=t[x].ok,t[t[x].ch[i]].fail=t[t[x].fail].ch[i],q.push(t[x].ch[i]);
    			else t[x].ch[i]=t[t[x].fail].ch[i];
    		}
    		t[x].ok&=t[t[x].fail].ok;
    	}
    }
    inline void dfs(int x){
    	if(vis[x]){puts("TAK");exit(0);}
    	vis[x]=true;
    	for(register int i=0;i<2;i++)if(t[t[x].ch[i]].ok)dfs(t[x].ch[i]);
    	vis[x]=false;
    }
    int main(){
    	scanf("%d",&n),t[1].ok=true;
    	while(n--)scanf("%s",s),S=strlen(s),ins();
    	build();
    	dfs(1);
    	puts("NIE");
    	return 0;
    }
    
  • 相关阅读:
    站点设计
    站点规划
    自动登录百度网盘
    windows下配置Python虚拟环境
    拖拽效果
    jQuery 基础
    js操作BOM
    js操作DOM
    selenium+js自动连接Magic_WiFi
    弹框
  • 原文地址:https://www.cnblogs.com/Troverld/p/12781148.html
Copyright © 2011-2022 走看看