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 xiongxianhe at 2010-05-24 15:27:21 on Problem 1204
#include <iostream>
using namespace std;

#define L 1000
#define C 1000
#define W 1000

//inline bool equal(char a,char b){return (a)==(b)? true:false;}

int l,c,w;//获取输入的行与列和单词数
char letters[L][C];//存放字母表
char word[W];
int wordLength=0;//单词的长度
char orientation;//获取方向
int firstline,firstcolumn;//第一次出现的位置

void InLetterTable()
{
	cin>>l>>c>>w;
	for(int i=0;i<l;i++)
	{
		for (int j=0;j<c;j++)
		{
			cin>>letters[i][j];
		}
	}
}

void InWord()
{
	char c;
	int i=0;
	while(cin>>noskipws>>c && c!='\n')
	{
		word[i]=c;
		i++;
	}
	wordLength=i;
}

void InFromFile()
{
	cin>>l>>c>>w;
	for(int i=0;i<l;i++)
		for(int j=0;j<c;j++)
			cin>>letters[i][j];	
}

char GA(int ll,int lc)
{
	int wordMove=0;
	while(word[wordMove]==letters[ll][lc] && ll>=0)
	{
		wordMove++;
		ll--;
	}
	if(wordMove==wordLength)
		return 'A';
	else
		return 0;
}

char GB(int ll,int lc)
{
	int wordMove=0;
	while(word[wordMove]==letters[ll][lc] && ll>=0 && lc<c)
	{
		wordMove++;
		ll--;
		lc++;
	}
	if(wordMove==wordLength)
		return 'B';
	else
		return 0;
}

char GC(int ll,int lc)
{
	int wordMove=0;
	while(word[wordMove]==letters[ll][lc]&& lc<c)
	{
		wordMove++;
		lc++;
	}
	if(wordMove==wordLength)
		return 'C';
	else
		return 0;
}

char GD(int ll,int lc)
{
	int wordMove=0;
	while(word[wordMove]==letters[ll][lc] && ll<l && lc<c)
	{
		wordMove++;
		ll++;
		lc++;
	}
	if(wordMove==wordLength)
		return 'D';
	else
		return 0;
}

char GE(int ll,int lc)
{
	int wordMove=0;
	while(word[wordMove]==letters[ll][lc] && ll<l)
	{
		wordMove++;
		ll++;
	}
	if(wordMove==wordLength)
		return 'E';
	else
		return 0;
}

char GF(int ll,int lc)
{
	int wordMove=0;
	while(word[wordMove]==letters[ll][lc] && ll<l && lc>=0)
	{
		wordMove++;
		ll++;
		lc--;
	}
	if(wordMove==wordLength)
		return 'F';
	else
		return 0;
}

char GG(int ll,int lc)
{
	int wordMove=0;
	while(word[wordMove]==letters[ll][lc] &&  lc>=0)
	{
		wordMove++;
		lc--;
	}
	if(wordMove==wordLength)
		return 'G';
	else
		return 0;
}

char GH(int ll,int lc)
{
	int wordMove=0;
	while(word[wordMove]==letters[ll][lc] && ll>=0 && lc>=0)
	{
		wordMove++;
		ll--;
		lc--;
	}
	if(wordMove==wordLength)
		return 'H';
	else
		return 0;
}


void Search()
{
	for (int i=0;i<l;i++)
	{
		for(int j=0;j<c;j++)
		{
			orientation=0;
			if(i>=wordLength-1)
				orientation=GA(i,j);
			if(orientation == 0 && i>=wordLength-1 && j<=c-wordLength+1)
				orientation=GB(i,j);
			if(orientation == 0 && j<=c-wordLength+1)
				orientation=GC(i,j);		
			if(orientation == 0 && i<=l-wordLength+1 && j<=c-wordLength+1)
				orientation=GD(i,j);
			if(orientation == 0 && i<=l-wordLength+1)
				orientation=GE(i,j);
			if(orientation == 0 && i<=l-wordLength+1 && j>=wordLength-1)
				orientation=GF(i,j);
			if(orientation == 0 &&  j>=wordLength-1)
				orientation=GG(i,j);
			if(orientation == 0 && i>=wordLength -1 && j>=wordLength -1)
				orientation=GH(i,j);
			if(orientation) 
			{
				firstline=i;
				firstcolumn=j;
				return;
			}
		}
	}
}

int main()
{
	firstcolumn=-1;
	firstline=-1;
	InFromFile();
	InWord();
	while(w--)
	{
		InWord();
		Search();
		cout<<firstline<<" "<<firstcolumn<<" "<<orientation<<endl;
	}
	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