UVa 10391 Compound Words

来源:互联网 发布:手机淘宝怎样赚集分宝 编辑:程序博客网 时间:2024/06/06 18:28

模板参考:http://blog.csdn.net/moyan_min/article/details/8445242

/*网上找到哈希模板,直接拿来用了= =正常查找肯定会超时的,所以要用哈希优化*/#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <vector>#using namespace std;const int maxn = 120000 + 10;char dict[maxn][105];int next[maxn],head[maxn];int len;int hash(char *p){    int count = 0;    int len = strlen(p);    for(int i = 0; i < len; i++) count = (count*10 + p[i] - 'a' + 1) % maxn;    return count;}bool search(char *p){    int count = hash(p);    int u = head[count];    while(u != -1)    {        if(strcmp(p,dict[u]) == 0) return true;        u = next[u];    }    return false;}int solve(char *s){    int l = strlen(s);    int ll;    char s1[25],s2[25];    memset(s1,0,sizeof(s1));    for(int i = 0; i < l; ++i)    {        s1[i] = s[i];        ll = 0;        for(int j = i+1; j < l; ++j)            s2[ll++] = s[j];        s2[ll] = '\0';//        printf("%s %s\n",s1,s2);        if(search(s1) && search(s2)) return 1;    }    return 0;}int main(){#ifndef ONLINE_JUDGE    freopen("in.txt","r",stdin);//    freopen("out.txt","w",stdout);#endif // ONLINE_JUDGE    len = 0;    memset(next,-1,sizeof(next));    memset(head,-1,sizeof(head));    while(scanf("%s",dict[len]) != EOF)    {        int cnt = hash(dict[len]);        next[len] = head[cnt];        head[cnt] = len;        len++;    }    solve(dict[1]);    for(int i = 0; i < len; ++i)    {        if(solve(dict[i])) printf("%s\n",dict[i]);    }    return 0;}


0 0
原创粉丝点击