Tries tree first question Codeforces 514c-watto and mechanism

Source: Internet
Author: User

Topic links

Test instructions

Enter a well-known string and a B-pending string. Ask if the string to be detected can be changed by a known string and only one letter will be changed. You can output yes, otherwise no.

Ideas:

Stores a known string with the trie tree. DFS changes one letter to see if it can match.

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>using namespacestd;Const intmax=6*1e5+Ten;CharStr[max];intval[max],ch[max][3];intSz//total number of nodesvoidInsert () {intu=0, n=strlen (str);  for(intI=0; i<n;i++){        intc=str[i]-'a'; if(!ch[u][c]) {//node does not existmemset (Ch[sz],0,sizeof(Ch[sz])); VAL[SZ]=0;//The previous node is not a word node now.ch[u][c]=sz++;//new Node} u=CH[U][C];//go down.} Val[u]=1;//This node is a word node}BOOLDfsintRtintPintDintLen) {    if(d>1){        return false; }    if(p==Len) {        if(d==1&&Val[rt]) {            return true; }        Else{            return false; }    }     for(intI=0;i<3; i++){        if(Ch[rt][i]) {//child nodes exist            if(Dfs (ch[rt][i],p+1, D+ (i!=str[p]-'a') , Len) {return true; }        }    }    return false;}intMain () {intb;  while(~SCANF ("%d%d",&a,&b)) {memset (Val,0,sizeof(Val)); memset (CH,0,sizeof(CH)); SZ=1;  for(intI=0; i<a;i++) {scanf ("%s", str); //puts (str);Insert (); }         for(intI=0; i<b;i++) {scanf ("%s", str); //puts (str);            if(Dfs (0,0,0, strlen (str))) {printf ("yes\n"); }            Else{printf ("no\n"); }        }    }    return 0;}

Tries tree first question Codeforces 514c-watto and mechanism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.