hdu1247+hdu1075 字典树基础

#include <stdio.h>//hdu1075
#include<string.h>
#include<algorithm>
using namespace std;
struct tree
{
	int flag,num;//flag用于标记是否为单词结尾
	tree *next[26];//指向26个字母
};
tree *root;
char s[1000000][20],b[4000];
void creat(char str[],int temp)
{
	int i,x,j;
	int len = strlen(str);
	tree *p = root;
	tree *q;
	for(i = 0; i < len; i++)
	{
		x = str[i] - 'a';
		if(!p->next[x])
		{
			q = (tree*)malloc(sizeof(tree));//这个一定不能少,也不能放在循环外面。我刚开始没理解,在这里调试了很久
			q->flag = 0;
			q->num = temp;//记录单词的编号,其实这个地方不记录,在循环完后记录flag = 1 的地方记录也可以
			for(j = 0; j < 26; j++)
			{
				q->next[j] = NULL;
			}
			p->next[x] = q;
			p = q;
		}
		else if(p->next[x]->flag==0) 
		{
			p->next[x]->num = temp;
			p = p->next[x];
		}
	}
	p->flag = 1;
}
int find(char str[])
{
	int x,i,len = strlen(str);
	tree *p = root;
	for(i = 0; i < len; i++)
	{
		x = str[i] - 'a';
		if(p->next[x]) p = p->next[x];
		else return 0;
	}
	if(p->flag) return p->num;
	else return 0;
}
int main()
{
//	freopen("t.txt","r",stdin);
	int i,j,len,text;
	char f[50],a[1000];
	root = (tree*)malloc(sizeof(tree));
	root->flag = 0;
	root->num = 0;
	for(i = 0; i < 26; i++)
	{
		root->next[i] = NULL;
	}
	gets(a);
	j = 1;
	while(1)
	{
		scanf("%s",s[j]);
		if(s[j][0] == 'E') break;
		j++;
		scanf("%s",s[j]);
		creat(s[j],j);
		j++;
	}	
	getchar();
	gets(a);
	while(1)
	{
		gets(b);
		if(b[0] == 'E') break;
		len = strlen(b);
		for(j = 0, i = 0; i < len; i++)
		{
			if(b[i] >= 'a' && b[i] <= 'z')
			{
				f[j++] = b[i];
			}
			else
			{
				f[j] = '\0';
				if(text = find(f))
				{
					printf("%s",s[text - 1]); 
				}
				else printf("%s",f);
				printf("%c",b[i]);
				memset(f,0,sizeof(f));
				j = 0;
			}
		}
		printf("\n"); 
	}
	return 0;
	
} 
#include<stdio.h>//hdu1247 类似的题目
#include<string.h>
#include<algorithm>
#include<stdlib.h>
using namespace std;
struct tree
{
	bool flag;
	tree* next[26];
};
tree * root;
char s[50005][20];
void creat(char str[])
{
	int x,i,j,len = strlen(str);
	tree* p = root;
	tree* q = (tree*)malloc(sizeof(tree));
	for(i = 0; i < len; i++)
	{
		x = str[i] - 'a';
		if(!p->next[x])
		{
			q = (tree*)malloc(sizeof(tree));
			for(j = 0; j < 26; j++)
			{
				q->next[j] = NULL;
			} 
			q->flag = 0;
			p->next[x] = q; 
			p = q;
		}
		else p = p->next[x];
	}
	p->flag= 1;
}
int search(char str[],int from,int end)
{
	int i,j,x;
	tree *p = root;
	for(i = from; i <= end; i++)
	{
		x = str[i] - 'a';
		if(p->next[x] == NULL) 
		{
			return 0;
		}
		p = p->next[x];
	}
	if(p->flag == 1) 
	{
		return 1;
	}
	return 0;
} 
int main()
{
//	freopen("t.txt","r",stdin);
	int len,i,j,num;

	root = (tree*)malloc(sizeof(tree));
	root->flag = 0;
	for(i = 0; i < 26; i++)
	{
		root->next[i] = NULL;
	}
	
	j = 0;
	while(~scanf("%s",s[j++]))
	{
		creat(s[j - 1]); 
	}
	num = j;
	for(i = 0; i < num; i++)
	{
		len = strlen(s[i]);
		if(len < 2) continue;
		for(j = 0; j < len - 1; j++)
		{
			if(search(s[i],0,j) && search(s[i],j+1,len-1))
			{
				 printf("%s\n",s[i]);
				 break;
			}
			
		}
	}
	return 0; 
} 


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值