POJ 2001 Shortest Prefixes G++

#include <iostream>
#include <vector>
#include <string>
using namespace std;
//字典树 典型题 
struct nod{
	nod* po[26];
	int shu;
	nod(){
		shu=0;
		for(int i=0;i<26;i++)
		{
			po[i]=NULL;
		}
	}
};
nod da[100000];
int js=1;
int main()
{
	/*
	for(int i=0;i<100000;i++)
	{
		da[i].shu=0;
		for(int j=0;j<26;j++)
		{
			da[i].po[j]=NULL;
		}		
	}*/
	string s;
	vector<string> ve;
	while(cin>>s)
	{
		ve.push_back(s);
		nod* tp=&da[0];
		for(int i=0;i<s.size();i++)
		{
			if(tp->po[s[i]-'a']==NULL)
			{
				//cout<<"NULL "<<s[i]<<endl;
				//cout<<"js "<<js<<endl;
				tp->po[s[i]-'a']=&da[js];				
				js++;
			}
			tp=tp->po[s[i]-'a'];//tp=tp->po[s[i]-'0'] 痛苦 
			//cout<<"hi "<<da[js-1].shu<<endl;
			//cout<<tp->shu<<endl;
			(tp->shu)++;	
			//cout<<"hello"<<endl;		
			//cout<<s[i]<<" "<<tp->shu<<" "<<tp<<endl;
		}
	}
	for(int i=0;i<ve.size();i++)
	{
		string s=ve[i];
		//cout<<s<<endl;
		nod* tp=&da[0];
		int wz=s.size();
		int flag=0;
		for(int i=0;i<s.size();i++)
		{
			
			//(tp->shu)--;
			tp=tp->po[s[i]-'a'];
			//cout<<s[i]<<" "<<tp->shu<<endl;
			if((tp->shu)==1)
			{
				if(flag==0)
				{
					flag=1;
					wz=i;					
				}
			}			
			
		}	
		//cout<<js<<endl;
		cout<<s<<" "<<s.substr(0,wz+1)<<endl;	
	}
	return 0;
} 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值