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] = '\0';
        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\n", str[i]);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/DearDongchen/p/7679048.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值