• [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;
    }
    
  • 相关阅读:
    多选下拉框带搜索(aps.net)
    asp.net无刷新上传(带预览)
    http免费升级https 攻略(超简单)
    用JS获取地址栏参数的方法
    C#生成二维码
    update更新另一个字段
    自适应瀑布型布局(手机,PC全兼容)
    微信扫码支付.net版本
    常用css3技巧
    Repeater 嵌套,子级Repeater获取 父级Repeater 中的值
  • 原文地址:https://www.cnblogs.com/Troverld/p/12781148.html
Copyright © 2020-2023  润新知