200- rare order

The forum to report every bug you find or tell us what you'd like to find in UVa OJ

Moderator: Board moderators

Locked
hazhir
New poster
Posts: 2
Joined: Fri Apr 11, 2008 7:33 pm

200- rare order

Post by hazhir »

I got WR for my code, I dont know anything about why it is wrong... :cry:
if you can help me,please...!

Code: Select all

#include <stdio.h>
#define offset 64

int main()
{
	char graph[27][27] = {0};	
	int exist[27] = {0};
	int count = 0;
	char *last = "";
	char *current = new char[21];	

	int flag = 0;
	while(gets(current),current[0] != '#')
	{			
		int k = 0;
		while(current[k])
		{								
			if(!exist[current[k] - offset])
				exist[current[k] - offset] = 1;
			k++;
		}

		k = 0;						
		while(last[k] && current[k])
		{
			if(last[k] != current[k])
			{
				count = graph[last[k] - offset][0];
				flag = 0;
				for(int i = 1;i<= count;i++)
					if(graph[last[k] - offset][i] == current[k])
					{
						flag = 1;
						break;
					}
				if(!flag)
				{
					graph[last[k] - offset][0]++;
					graph[last[k] - offset][graph[last[k] - offset][0]] = current[k];
				}
				break;
			}
			else
				k++;
		}							

		last = current;		
		current = new char[21];
	}

	//DFS and TOPOLOGICAL SORT
	int f[27] = {0};
	int d[27] = {0};
	int color[27] = {0};
	int stack[27] = {0};
	int counter[27];
	for(int i = 1;i<= 26;i++)
		counter[i] = 1;
	int sPoint = 0;
	int time = 0;
	int k;
	for(int i = 1;i <= 26;i++)
	{
		if(exist[i] == 1)
		{
			k = i;
			if(color[k] == 0)
			{
				do{
					if(color[k] == 0)
					{
						color[k] = 1;
						time++;
						d[k] = time;
			   			stack[sPoint] = k;
						sPoint++;
					}
					if(counter[k] <= graph[k][0] && color[graph[k][counter[k]] - offset] == 0)
					{	
						counter[k]++;						
						k = graph[k][counter[k]-1] - offset;						
					}
					else if(sPoint)
					{
						sPoint--;
						k = stack[sPoint];
						color[k] = 2;
						time++;
						f[k] = time;
					}
				}while(sPoint);
			}
		}
	}
	
	k = 0;
	int result[27];
	char letter[27];
	for(int i = 1;i <= 26;i++)
	{
		if(exist[i] == 1)
		{
			result[k] = f[i];
			letter[k] = i + offset;
			k++;
		}
	}

	int max = 0;
	int maxindex;
	for(int i = 0;i<=k-2; i++)
	{
		max = result[i];
		maxindex = i;
		for(int j = i+1;j<=k-1;j++)
		{
			if(result[j] > max)
			{
				max = result[j];
				maxindex = j;
			}
		}
		char temp = letter[maxindex];
		letter[maxindex] = letter[i];
		letter[i] = temp;

		result[maxindex] = result[i];
		result[i] = max;
	}

	for(int i = 0;i<=k-1;i++)
		printf("%c",letter[i]);
	printf("\n");
	return 0;
}
Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Re: 200- rare order

Post by Jan »

Find the correct place to post a problem. And before that you should search the problem first.
Ami ekhono shopno dekhi...
HomePage
Locked

Return to “Bugs and suggestions”