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

一直MLE。。。。。蛋疼。。谁改改吧

Posted by yizer at 2012-03-20 20:09:39 on Problem 1798
#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <cctype>
#include <vector>
using namespace std;

#define MAXN 75005
#define MLEN 52

const int cheat[26] = {5,7,6,3,0,4,9,9,6,1,7,8,5,1,8,8,1,2,3,4,7,6,2,2,3,9};

struct tans
{
	vector<string> data;
};

vector<tans> ANS;

char str[MAXN][MLEN];
vector<int> st[MLEN];
int buff[MAXN];

struct Tire
{
	Tire *child[10];
	vector<int> jmp;
}*root;

Tire * new_Tire()
{
	Tire *ret = new Tire;
	ret->jmp.clear();
	memset(ret->child,0,sizeof(ret->child));
	return	ret;
}

inline int letter_to_num(char x)
{
	return	cheat[x-'a'];
}

void insert(Tire *root,char *str,int&pos)
{
	if (*str)
	{
		if (isalpha(*str))
		{
			int tmp = letter_to_num(tolower(*str));
			if (!root->child[tmp])
				root->child[tmp] = new_Tire();
			insert(root->child[tmp],str+1,pos);
		}
		else
			insert(root,str+1,pos);
	}
	else
		root->jmp.push_back(pos);
}

void track(vector<int>*st,int &pos,int &len,char *str,Tire*root)
{
	int t = pos;
	Tire *p = root;
	while (t < len && p->child[str[t]-'0'])
	{
		p = p->child[str[t]-'0'];
		if (p->jmp.size())
			st[t+1].push_back(pos);
		t++;
	}
}

tans ttt;

void make_up(int *buff,int pos,Tire*root,char*pstr)
{
	if (pos)
	{
		Tire *p = root;
		int t = buff[pos];
		while (t < buff[pos-1])
		{
			p = p->child[pstr[t]-'0'];
			t++;
		}
		for (int i=0;i<p->jmp.size();i++)
		{
			string tmp = str[p->jmp[i]];
			ttt.data.push_back(tmp);
			make_up(buff,pos-1,root,pstr);
			ttt.data.pop_back();
		}
	}
	else
		ANS.push_back(ttt);
}

void find_solve(vector<int>*st,int pos,int deep,Tire*root,char *str)
{
	buff[deep] = pos;
	if (pos != 0)
		for (int i=0;i<st[pos].size();i++)
			find_solve(st,st[pos][i],deep+1,root,str);
	else
	{
		ttt.data.clear();
		make_up(buff,deep,root,str);
	}
}

bool cmp(const tans a,const tans b)
{
		
	for (int i=0;i<min(a.data.size(),b.data.size());i++)
		if (strcmp(a.data[i].c_str(),b.data[i].c_str())<0)
			return	true;
		else
		if (strcmp(a.data[i].c_str(),b.data[i].c_str())>0)
			return	false;
	return	a.data.size()<b.data.size();
}

void DESTORY(Tire*root)
{
	if (root)
	{
		for (int i=0;i<10;i++)
			DESTORY(root->child[i]);
		delete root;
	}
}

int main()
{
	#ifndef ONLINE_JUDGE
	freopen("data.in","r",stdin);
//	freopen("data.out","w",stdout);
	#endif

	int nCase;
	scanf("%d",&nCase);
	for (int nc=0;nc<nCase;nc++)
	{
		
		root = new_Tire();
		int ndic , np;
		scanf("%d",&ndic);
		for (int i=0;i<ndic;i++)
			scanf("%s",str[i]),insert(root,str[i],i);
		scanf("%d",&np);
		printf("Scenario #%d:\n",nc+1);
		while (np--)
		{
			ANS.clear();
			char pt[51];
			char new_pt[51];
			scanf("%s",pt);
			int p = 0;
			int len = strlen(pt);

			for (int i=0;i<len;i++)
				if (isdigit(pt[i]))
					new_pt[p++] = pt[i];
			
			for (int i=0;i<MLEN;i++)
				st[i].clear();

			st[0].push_back(0);
			
			for (int i=0;i<p;i++)
				if (st[i].size())
					track(st,i,p,new_pt,root);
			
			
			if (st[p].size())
			{
				find_solve(st,p,0,root,new_pt);
				sort(ANS.begin(),ANS.end(),cmp);
				for (int i=0;i<ANS.size();i++)
				{
					printf("%s:",pt);
					for (int j=0;j<ANS[i].data.size();j++)
						printf(" %s",ANS[i].data[j].c_str());
					puts("");
				}
			}
			else
				printf("%s cannot be encoded.\n",pt);
		}
		puts("");
		DESTORY(root);
	}

	#ifndef ONLINE_JUDGE
	cerr << "DONE." << endl;
	#endif
	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