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

我会说我大小写错了吗

Posted by cotton at 2012-06-30 21:10:48 on Problem 3630
贴一下代码吧
//poj3630 trie
//Can't judge if two strings are equal
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <climits>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <cassert>

using namespace std;


const int MAXN = 10000 + 2, MAXL = 10 + 2;

struct Node{
	int finish;
	Node* son[10];
	Node() {  }
};

Node node[MAXN * MAXL];
Node *Root;
int Total = 0;

void insert(char *c, Node *v)
{
	//cerr<<c<<endl;
	int p = (*c) - '0';
	if (*c == 0){
		++(v->finish);
		return;
	}
	assert( p >= 0 );
	assert( p < 10 );
	assert( *c != 0 );
	if ( v->son[p] == NULL )
		v->son[p] = &node[++Total];
		
	insert(c+1, v->son[p] );
	
}

bool have_prefix(Node *v)
{
	bool found = false;
	if ( v->finish != 0 ){
		for (int i = 0; !found && i < 10; ++i)
			found = (v->son[i] != NULL);
	}
	else{
		for (int i = 0; !found && i < 10; ++i)
			if (v->son[i] != NULL)
				found = have_prefix(v->son[i]);
	}
	return found;
}

int main()
{
	char ori[MAXL];
	int testcase, n;
	scanf("%d", &testcase);
	while (testcase--){
		memset(node, 0, sizeof(node) );
		Root = node;
		Total = 0;
		
		scanf("%d", &n);
		while (n--){
			scanf("%s", ori);
			insert(ori, Root);
		}
		
		if ( !have_prefix(Root) )
			printf("YES\n");
		else
			printf("NO\n");
	}//while testcase--
	
	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