HDOJ 1075 What Are You Talking About

14 篇文章 0 订阅
7 篇文章 0 订阅

开始不知道gets也像getchar一样会吞scanf的最后字符,导到致一直多读入了一个换行,一直PE。

换了个姿势写三叉树。

#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 6000010;
int Node[maxn][3], cnt[maxn];
char elem[maxn];
int top, sstp;
char ss[maxn][12];
int newNode(char ch)
{
	top ++;
	cnt[top] = Node[top][0] = Node[top][1] = Node[top][2] = 0;
	elem[top] = ch;
	return top;
}
void insert(char str[])
{
	int len = strlen(str);
	int p = 1;
	for(int i = 0; i < len; i++)
	{
		while(elem[p] != str[i])
		{
			if(str[i] < elem[p])
			{
				if(!Node[p][0])Node[p][0] = newNode(str[i]);
				p = Node[p][0];
			}
			else 
			{
				if(!Node[p][1])Node[p][1] = newNode(str[i]);
				p = Node[p][1];
			}
		}
		if(!Node[p][2])Node[p][2] = newNode('\0');
		p = Node[p][2];
	}
	cnt[p] = sstp;
}

int find(char str[])
{
	int p = 1;
	int len = strlen(str);
	for(int i = 0; i < len; i++)
	{
		while(str[i] != elem[p])
		{
		if(str[i] < elem[p])
			if(!Node[p][0])return 0;
			else p = Node[p][0];
		else 
			if(!Node[p][1])return 0;
			else p = Node[p][1];
		}
		if(!Node[p][2])return 0;
		else p = Node[p][2];
	}
	return cnt[p];
}
int main()
{
	char str[15];
	char tmp[3004];
	sstp = 1;
	scanf("%s",str);
	top = 0;
	newNode('\0');
  	while(scanf("%s",ss[sstp]))
	{
		if(ss[sstp][0] == 'E')break;
		scanf("%s",str);
		insert(str);	
		sstp ++;
	}
	getchar();
	gets(tmp);
	while(1)
	{
		gets(tmp);
		if(tmp[0] == 'E')break;
		int i,len, t;
        len = strlen(tmp);
		t = 0;
        for(i = 0; i < len; i++)
        {
			if(tmp[i] < 'a' || tmp[i] > 'z')
			{
				str[t] = '\0';
				t = 0;
				sstp = find(str);
				if(sstp)printf("%s",ss[sstp]);
				else printf("%s", str);
				printf("%c",tmp[i]);
			}
			else str[t++] = tmp[i];
        }
		printf("\n");
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值