Cod sursa(job #1343249)

Utilizator raduneagoeNeagoe Radu raduneagoe Data 15 februarie 2015 01:44:31
Problema Convertor Scor 70
Compilator java Status done
Runda rosedu_cdl_2015 Marime 4.09 kb
import java.io.*;
import java.util.Scanner;

public class Main
{	
	public class CompactCharSequence implements CharSequence, Serializable 
	{
		  static final long serialVersionUID = 1L;
		  
		  private static final String ENCODING = "ISO-8859-1";
		  private final int offset;
		  private final int end;
		  private final byte[] data;

		  public CompactCharSequence(String str) 
		  {
			  try 
			  {
				  data = str.getBytes(ENCODING);
				  offset = 0;
				  end = data.length;
			  } catch (UnsupportedEncodingException e) 
			  {
				  throw new RuntimeException("Unexpected: " + ENCODING + " not supported!");
			  }
		  }

		  public char charAt(int index) 
		  {
			  int ix = index+offset;
			  if (ix >= end) 
				  throw new StringIndexOutOfBoundsException("Invalid index " + index + " length " + length());
			  return (char) (data[ix] & 0xff);
		  }

		  public int length() 
		  {
			  return end - offset;
		  }
		  
		  private CompactCharSequence(byte[] data, int offset, int end) 
		  {
			  this.data = data;
			  this.offset = offset;
			  this.end = end;
		  }

		  @Override
		  public CharSequence subSequence(int start, int end)
		  {
			  if (start < 0 || end > (this.end-offset)) 
				  throw new IllegalArgumentException("Illegal range " + start + "-" + end + " for sequence of length " + length());
			  return new CompactCharSequence(data, start + offset, end + offset);
		  }
		  
		  public String toString() 
		  {
			  try 
			  {
				  return new String(data, offset, end-offset, ENCODING);
			  } catch (UnsupportedEncodingException e) 
			  {
				  throw new RuntimeException("Unexpected: " + ENCODING + " not supported");
			  }
		  }

		public int indexOf(char c)
		{
			for(int i = 0; i < end; i++)
			{
				if(data[i] == c)
					return i;
			}
			return 0;
		}

		public int indexOf(char c, int a)
		{
			for(int i = a; i < end; i++)
			{
				if(data[i] == c)
					return i;
			}
			return 0;
		}
	}
	
	public static int charAppears(CharSequence s, char ch)
	{
		int counter = 0;
		for(int i = 0; i < s.length(); i++)
		{
			if(s.charAt(i) == ch)
				counter++;
		}
		return counter;
	}
	
	public static CharSequence deleteChar(CharSequence str)
	{	
		while(str.charAt(0) == ' ' || str.charAt(0) == '"' || str.charAt(str.length() - 1) == ' ' || str.charAt(str.length() - 1) == '"' || str.charAt(str.length() - 1) == '}' || str.charAt(str.length() - 1) == ',')
		{
			if(str.charAt(0) == ' ' || str.charAt(0) == '"')
				str = str.subSequence(1,str.length());

			if(str.charAt(str.length() - 1) == ' ' || str.charAt(str.length() - 1) == '"' || str.charAt(str.length() - 1) == '}' || str.charAt(str.length() - 1) == ',')
				str = str.subSequence(0,str.length() - 1);
		}
		return str;
	}
	
	public static void main(String[] args) throws IOException
	{
		@SuppressWarnings("resource")
		CompactCharSequence JSON = new Main().new CompactCharSequence(new Scanner(new FileInputStream("convertor.in")).useDelimiter("\\Z").next().replaceAll("(\\r|\\n)", ""));
		PrintWriter writer = new PrintWriter("convertor.out");
		
		// prima linie
		int a = JSON.indexOf('"') + 1, b = JSON.indexOf(':') - 1;
		int m = charAppears(JSON.subSequence(JSON.indexOf(':'), JSON.indexOf('}')), ',') + 1; //nr virgulelor = nr coloanelor
		int n = charAppears(JSON, '}'); //nr '}' = nr liniilor, fara prima linie
		
		for (int i = 0; i < m; i++)
		{
			writer.print(deleteChar(JSON.subSequence(a, b)) + ",");
			
			a = JSON.indexOf(',', a) + 1;
			b = JSON.indexOf(':', b + 2) - 1;
		}
		
		//urmatoarele linii
		a = b = 0;
		for(int i = 0; i < n; i++)
		{
			writer.println();
			
			for(int j = 0; j < m; j++)
			{
				a = JSON.indexOf(':', a) + 1;
				b = JSON.indexOf(',', b) + 1;
				try //pe ultima linie nu voi mai avea "}," si va da eroare
				{
					writer.print(deleteChar(JSON.subSequence(a, b)) + ",");
				} 
				catch (StringIndexOutOfBoundsException e)
				{
					b = JSON.toString().lastIndexOf("}");
					writer.print(deleteChar(JSON.subSequence(a, b)) + ",");
				}
			}
		}
		writer.close();
	}
}