Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

第一次Trie还是链表构造,成就感~

Posted by Mikucon at 2012-01-30 20:53:47 on Problem 2513
#include <iostream>
#include <cstring>
using namespace std;
int lst,num,l,m,n1,n2,tag,f1,f2,tmp;
#define mxn 5200001
//trie末尾lst,单词编号num,单词长度l, 单词数m
int tr[mxn];//第一个子节点的编号
int n[mxn];//单词序号
int w[mxn];//w[i]表示i位子木为w[i]
int nxt[mxn];//连接同一邻接父节点的下一个兄弟节点
int f[mxn/10+1];//父亲标识
int d[mxn/10+1];
char s1[25],s2[25],now[25];
void insert(int x,int p)
{
	if (p==l) 
	{
		if (n[x]==0) n[x]=++m;
		num=n[x];
		return;
	}
	tag=now[p]-'a'+1;//当前查找字母
	if (tr[x]==0)
	{
		tr[x]=++lst;
		w[lst]=tag;
		insert(lst,p+1);
	}
	else {
		tmp=tr[x];
		while (nxt[tmp]!=0&&w[tmp]!=tag) tmp=nxt[tmp];
		if (w[tmp]==tag) insert(tmp,p+1);
		else {
			nxt[tmp]=++lst;
			w[lst]=tag;
			insert(lst,p+1);
		}
	}
}
bool check()
{
	int root=0,c=0;
	for (int i=1;i<=m;i++)
	{
		int ex=f[i];
		while (f[ex]!=ex) ex=f[ex];
		if (d[i]&1)c++;
		if (root==0||root==ex) root=ex;
		else return false;
	}
	return (c==0||c==2);
}
int main()
{
	memset(f,0,sizeof(f));
	memset(tr,0,sizeof(tr));
	memset(n,0,sizeof(n));
	memset(nxt,0,sizeof(nxt));
	memset(w,0,sizeof(w));
	memset(d,0,sizeof(d));
	lst=m=0;
	for (int i=1;i<=mxn/10;i++) f[i]=i;
	while (scanf("%s%s",s1,s2)!=EOF)
	{
		strcpy(now,s1); l=strlen(s1);
		insert(0,0); n1=num;
		strcpy(now,s2); l=strlen(s2);
		insert(0,0); n2=num;
		d[n1]++;d[n2]++;
		while (f[n1]!=n1) n1=f[n1];
		while (f[n2]!=n2) n2=f[n2];
		f[n2]=n1;
	}
	if (check())cout<<"Possible\n";
	else cout<<"Impossible\n";
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator