首页 > 其他 > 详细

Phone List HDU1671

时间:2019-01-31 17:28:11      阅读:129      评论:0      收藏:0      [点我收藏+]

字典树的包含与不包含关系

技术分享图片
#include<bits/stdc++.h>
using namespace std;
int trie[400100][10];
int sum[400100];
int pos=0;

bool insert1( char *word )
{
    int root=0;
    for(int i=0;i<strlen(word);i++)
    {   int ch=word[i]-0;
        if(trie[root][ ch ]==0)
            trie[root][ ch ]=++pos;
        if(sum[root]==2&&root!=0)return false;
          sum[root]=1;
        root=trie[root][ch];


    }
      if(sum[root]!=1)  sum[root]=2;
      else  return false;
  return true;
}

int main()
{
    int cas;scanf("%d",&cas);
    while(cas--)
    {   
        pos=0;
        int n;
        memset(sum,false,sizeof(sum));
        memset(trie,0,sizeof(trie));
        char word[11];
        scanf("%d",&n);
        int ok=1;
        while(n--)
        {
            scanf("%s",word);
            if(ok==0)continue;
           if(!insert1(word)){ok=0;}

        }
        if(ok)printf("YES\n");
        else printf("NO\n");

    }

}
View Code

 

Phone List HDU1671

原文:https://www.cnblogs.com/bxd123/p/10342842.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!