HDU-1251

字典树还是要写的,自己得重新拾起来自己丢掉的知识,要不然还真是不行的.

就像刘飘说的那样,我也一直这样坚信着,自己答应别人的事情,就一定要完成.

男人最可贵的就是诺言!

#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

char str[15];

const int sta = 'a';

struct node{
	node *next[26];
	int n;
}*root;

void init (node *t)
{
	memset (t -> next, 0 , sizeof (t -> next));
	t -> n = 0;
}

void Insert (char *s)
{
	node *t = root;
	node *newnode;
	int n = strlen (s);
	for (int i = 0; i < n; i++)
	{
		if (t -> next[s[i] - sta] == 0)
		{
			newnode = new node;
			init (newnode);
			t -> next[s[i] - sta] = newnode;
			t = newnode;
			t -> n++;		
		}
		else
		{
			t = t -> next[s[i] - sta];
			t -> n++;	
		}
	}
}

int find (char *s)
{
	node *t = root;
	int n = strlen (s);
	for (int i = 0; i < n; i++)
	{
		if (t -> next[s[i] - sta] == 0)
		{
			return 0;
		}
		else
		{
			t = t -> next[s[i] - sta];
		}
	}
	return t -> n;
}
			
			

int main()
{
	root = new node;
	init (root);//注意一定要初始化,要不然的话会错的.晕 
	while (gets (str))
	{
		if (strlen (str) == 0)
		{
			break;
		}
		Insert (str);
	}
	while (scanf ("%s", str) != EOF)
	{
		int ans = find (str);
		cout << ans << endl;
	}
	return 0;
}
	



看了别人的比较神的MAP写的,感觉非常的不错,,自己也写了一遍,,开始是用string写的,发现不可以.收入回车

所以自己又重新用str写了一遍,,,真的我发现能够实现的方法真的很多,所以,千万别着急自己不如别人.要韬光养晦,

成功从来都不是以早晚决定的,而是以大小!

#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;


int main()
{
	char str[15];
	char pat[15];
	map  m;
	while (gets(str))
	{
		int n = strlen (str);
		if (n == 0)
		{

			break;
		}
		for (int i = n; i > 0; i--)
		{
			str[i] = '\0';
			m[str]++;
//			cout << str << " " << m[str] <> pat)
	{
		cout << m[pat] << endl;
	}
	return 0;
}
			
	





你可能感兴趣的:(字典树)