Best Cow Line, Gold

From Progteam

Revision as of 21:33, 21 September 2008 by MuGMaN (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


Best Cow Line, Gold
Problem Number 3623
Sorter: Eric
Source: USACO 2007 December Gold
Link: http://acm.pku.edu.cn/JudgeOnline/problem?id=3623




Best Cow Line, Gold is problem number 3623 on the Peking University ACM site.



Memo

  • Works with sample input
  • Gives me a run-time error (Probably because of indices of the array)
  • Need to consider the case in which the front and the rear are identical


import java.util.*;

public class Main
{
	public static Scanner in;

	public static void main(String[] args)
	{
		in = new Scanner(System.in);
		doStuff();
	}

	public static void doStuff()
	{
		int numCow = in.nextInt();
		char[] initial = new char[numCow];
		
		for (int i = 0; i < numCow; i++)
		{
			initial[i] = in.next().charAt(0);
		}
		
		solve(initial);
	}

	public static void solve(char[] initial)
	{
		int size = initial.length;
		String result = "";
		
		int i = 0;
		int j = size - 1;
		
		for (int k = 0; k < size; k++)
		{
			char front = initial[i];
			char rear = initial[j];
			
			if (front < rear)
			{
				result += front;
				i++;
			}
			else if ((front == rear) && ((j  != 0) || (i  != size - 1)))
			{
				if (initial[i + 1] <= initial[j - 1])
				{
					result += front;
					i++;
				}
				else
				{
					result += rear;
					j--;
				}
			}
			else
			{
				result += rear;
				j--;
			}
		}
		System.out.println(result);
	}
}
Personal tools