现在的位置: 首页 > 综合 > 正文

Compound Words 10391

2013年06月07日 ⁄ 综合 ⁄ 共 1350字 ⁄ 字号 评论关闭

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>   
#include <map>
#include <string>  
#include <climits> 
#include <set>
#include <string> 
#include <sstream>
#include <utility>
#include <ctime>

using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::istringstream;
using std::make_pair;
using std::greater;

const int MOD(1313131);
const int MAXN(120010);

char str[MAXN][25];

struct HASH
{
	int head[MOD];
	int next[MAXN];
	int rear;
	int seed;
	void init()
	{
		memset(head, -1, sizeof(head));
		rear = 0;
		seed = 131;
	}
	
	int hash(char *p)
	{
		int ret = 0;
		while(*p)
		{
			ret = ret*seed+*p++;
		}
		return (ret&0x7fffffff)%MOD;
	}

	void insert(char *p)
	{
		int h = hash(p);
		next[++rear] = head[h];
		head[h] = rear;
	}

	bool search(char *p)
	{
		int h = hash(p);
		for(int i = head[h]; i != -1; i = next[i])
			if(strcmp(str[i], p) == 0)
				return true;
		return false;
	}
};

HASH ht;
char ts[25];

int main()
{
	ht.init();
	int count = 1;
	while(~scanf("%s", str[count]))
		ht.insert(str[count++]);
	for(int i = 1; i <= count; ++i)
	{
		strcpy(ts, str[i]);
		for(char *tp = ts+1; *tp != '\0'; ++tp)
		{
			bool flag;
			char temp = *tp;
			*tp = '\0';
			flag = ht.search(ts);
			if(flag)
			{
				*tp = temp;
				if(ht.search(tp))
				{
					printf("%s\n", ts);
					break;
				}
			}
			*tp = temp;
		}
	}
	return 0;
}

抱歉!评论已关闭.