UVa 10391 - Compound Words (字符串hash)

Time Limit:3000MS Memory Limit:Unknown 64bit IO Format:%lld & %llu

[]  [Go Back]  [Status]  

Description

Download as PDF

Problem E: Compound Words

You are to find all the two-word compound words in a dictionary. A two-word compound word is a word in the dictionary that is theconcatenation of exactly two other words in the dictionary.

Input

Standard input consists of a number of lowercase words, one per line,in alphabetical order. There will be no more than 120,000 words.

Output

Your output should contain all the compound words, one per line, inalphabetical order.

Sample Input

a
alien
born
less
lien
never
nevertheless
new
newborn
the
zebra

Sample Output

alien
newborn

题意:

给定一个字典,找出其中所有的复合词,即恰好由两个单词连接而成的单词。


思路:

先hash存下每个单词 然后枚举每个单词 对于单词i,枚举单词拆分的地方,然后看两个子单词有没有在字典里面



#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

using namespace std;

//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif

#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)

const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double PI = (4.0*atan(1.0));

const int maxn = 120000 + 20;
int Head[maxn];
int Next[maxn];
string data[maxn];
int cnt;

int ELFhash(char * s) {
    unsigned long h = 0;
    while(*s) {
        h = (h << 4) + *s++;
        unsigned long g = h & 0xf0000000L;
        if(g) h ^= g >> 24;
        h &= ~g;
    }
    return h % maxn;
}

void init() {
    memset(Head, -1, sizeof(Head));
    memset(Next, -1, sizeof(Next));
    cnt = 0;
}

void insert(char * s) {
    int key = ELFhash(s);
    data[cnt] = string(s);
    Next[cnt] = Head[key];
    Head[key] = cnt;
    cnt++;
}

bool find(char * s) {
    int key = ELFhash(s);
    int u = Head[key];
    while(u != -1) {
        if(strcmp(data[u].c_str(), s) == 0) return true;
        u = Next[u];
    }
    return false;
}

void subString(char * sub, char * s, int st, int ed) {
    int len = ed - st + 1;
    for(int i=0; i<len; i++) {
        sub[i] = s[st+i];
    }
    sub[len] = '\0';
}

char ins[maxn][100];
char tmp1[100];
char tmp2[100];

int main() {
    int n = 0;

    init();
    while(scanf("%s", ins[n]) != EOF) {
        insert(ins[n++]);
    }
    for(int i=0; i<n; i++) {
        int len = strlen(ins[i]);
        for(int j=0; j<len-1; j++) {
            subString(tmp1, ins[i], 0, j);
            subString(tmp2, ins[i], j+1, len-1);
            if(find(tmp1) && find(tmp2)) {
                puts(ins[i]);
                break;
            }
        }
    }

    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值