zoukankan      html  css  js  c++  java
  • hash表

    https://oj.neu.edu.cn/problem/60

    学习哈希姿势

    #include<cstdio>
    #include<cstring>
    using namespace std;
    
    const int maxn = 120007, HASH = 119993;
    int head[HASH], nxt[maxn], INDEX;
    char str[maxn][20];
    int getkey(char* s){
        int sum = 0;
        for(int i = 0; s[i]; i++){
            sum = (sum*13+s[i])%HASH;
        }
        return sum%HASH;
    }
    void insert(int pos){
        int h = getkey(str[pos]);
        int u = head[h];
        while(u){
            if(strcmp(str[u], str[pos]) == 0){
                //puts("chongfule");
                return;
            }
            u = nxt[u];
        }
        nxt[pos] = head[h];
        head[h] = pos;
    }
    bool inside(char* s){
        int h = getkey(s);
        int u = head[h];
        while(u){
            if(strcmp(str[u], s) == 0){
                return true;
            }
            u = nxt[u];
        }
        return false;
    }
    bool compound(int i){
        int len = (int)strlen(str[i]);
        char a[20], b[20];
        for(int j = 1; j < len; j++){
            strcpy(a, str[i]);
            a[j] = '';
            strcpy(b, str[i]+j);
            if(inside(a) && inside(b))
                return true;
        }
        return false;
    }
    
    int main(){
        INDEX = 0;
        while(scanf("%s", str[++INDEX])!=EOF){
            insert(INDEX);
        }
        for(int i = 1; i <= INDEX; i++){
            if(compound(i))
                printf("%s
    ", str[i]);
        }
        return 0;
    }
    搞图论是没有用的,转行做数学题了hh
  • 相关阅读:
    暴力程序之回文子串
    关于取消同步带来问题的样例
    JavaScript之Date
    JavaScript之array
    智破连环阵
    超长数字串
    无向图最短路径
    扫雷
    n!最末尾非0数
    计算程序运行时间
  • 原文地址:https://www.cnblogs.com/DearDongchen/p/7679048.html
Copyright © 2011-2022 走看看