UVa 10391 Compound Words

模板参考: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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值