其他分享
首页 > 其他分享> > Shortest Prefixes

Shortest Prefixes

作者:互联网

Aimee

很简单的字典树

不知道为什么,写了struct就会TLE

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstring>
using namespace std;
struct tr{
	int vis;
	string str;
	int ne[31];
}tree[100001];
int Aimee=1;
int x;
int Ai;
char ss[1101][50];
void add(char s[]){
	int Archie=0;
	for(int i=0;i<s[i];++i){
	int	x=s[i]-'a';
		if(!tree[Archie].ne[x]){
			Aimee++;
			tree[Archie].ne[x]=Aimee;
		}
		Archie=tree[Archie].ne[x];
		tree[Archie].vis++;	
	}
}
string find(char s[]){
	int Archie=0;
	string ans;
	for(int i=0;i<s[i];++i){
	int	x=s[i]-'a';
		if(tree[Archie].vis==1){
			return ans;
		}
		ans+=s[i];
		Archie=tree[Archie].ne[x];
	}
	return ans;
}
int main(){
	for(int i=0;i<100001;++i){
		for(int j=0;j<=26;++j)
		tree[i].ne[j]=0;
		tree[i].vis=0;
	}
	while(scanf("%s",ss[Ai])!=EOF){
		add(ss[Ai]);
		Ai++;
	}
	for(int i=0;i<Ai;++i){
		cout<<ss[i]<<" "<<find(ss[i])<<endl;
	}
	return 0;
}

标签:char,struct,int,namespace,Prefixes,Aimee,include,Shortest
来源: https://www.cnblogs.com/For-Miku/p/14076616.html