zoukankan      html  css  js  c++  java
  • [洛谷2814]家谱

    思路:
    字符串哈希,然后用普通的并查集维护即可。

     1 #include<cstdio>
     2 #include<cctype>
     3 #include<cstring>
     4 const int mod=19260817;
     5 char name[mod][7];
     6 inline int hash(char *s) {
     7     int n=strlen(s),ret=0;
     8     for(int i=0;i<n;i++) {
     9         ret=(ret*26+tolower(s[i])-'a')%mod;
    10     }
    11     strcpy(name[ret],s);
    12     return ret;
    13 }
    14 class DisjointSet {
    15     private:
    16         int anc[mod];
    17     public:
    18         DisjointSet() {
    19             for(int i=0;i<mod;i++) anc[i]=i;
    20         }
    21         int Find(const int x) {
    22             return x==anc[x]?x:anc[x]=Find(anc[x]);
    23         }
    24         void Union(const int x,const int y) {
    25             anc[Find(x)]=Find(y);
    26         }
    27 };
    28 DisjointSet s;
    29 int main() {
    30     int par;
    31     char c,str[8];
    32     while(~scanf("%s",str)) {
    33         c=str[0];
    34         for(int i=0;i<7;i++) {
    35             str[i]=str[i+1];
    36         }
    37         if(c=='#') {
    38             par=hash(str);
    39         }
    40         if(c=='+') {
    41             s.Union(hash(str),par);
    42         }
    43         if(c=='?') {
    44             printf("%s %s
    ",str,name[s.Find(hash(str))]);
    45         }
    46         if(c=='$') break;
    47     }
    48     return 0;
    49 }
  • 相关阅读:
    Python生成器
    Python迭代器
    Python异常处理
    Python面向对象进阶
    Python面向对象基础
    Python闭包和装饰器
    Python函数
    Python文件操作
    Python深浅拷贝
    Python的列表&元组&字典&集合
  • 原文地址:https://www.cnblogs.com/skylee03/p/7382603.html
Copyright © 2011-2022 走看看