Codeforces Round #291 (Div. 2)C.Watto and Mechanism——字典樹+dfs,

來源:互聯網
上載者:User

Codeforces Round #291 (Div. 2)C.Watto and Mechanism——字典樹+dfs,

http://codeforces.com/contest/514

n個字串m個詢問,判斷詢問的字串能否通過修改其中的一個字元使得變成給定的n個字串中的某一個

注意:必須修改一個字元

#include<bits/stdc++.h>const int maxnode=6e5+100;const int sigma_size=3;using namespace std;int n,m;int ch[maxnode][sigma_size],val[maxnode];int sz;void init(){    memset(ch,0,sizeof ch);    memset(val,0,sizeof(val));    sz=1;}void insert(char *s){    int u=0,l=strlen(s);    for(int i=0; i<l; ++i) {        int c=s[i]-'a';        if(!ch[u][c]) {            ch[u][c]=sz++;        }        u=ch[u][c];    }    val[u]=1;}char s[maxnode];bool dfs(char *s,int rt,int p,int d,int l){    if(d>1) return false;    if(p==l){        if(d==1&&val[rt]) return true;        return false;    }    for(int i=0;i<sigma_size;++i){        if(ch[rt][i]){            if(dfs(s,ch[rt][i],p+1,d+(i!=s[p]-'a'),l)) return true;        }    }    return false;}int main(){    init();    scanf("%d%d",&n,&m);    for(int i=0; i<n; ++i) {        scanf("%s",s);        insert(s);    }    for(int i=0; i<m; ++i) {        scanf("%s",s);        if(dfs(s,0,0,0,strlen(s))) printf("YES\n");        else printf("NO\n");    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.