Cod sursa(job #1361038)

Utilizator tudoras8tudoras8 tudoras8 Data 25 februarie 2015 19:17:58
Problema Convertor Scor 70
Compilator java Status done
Runda rosedu_cdl_2015 Marime 1.41 kb
import java.io.*;
import java.util.Scanner;
import java.util.regex.*;

public class Main {
	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(new FileInputStream("convertor.in"));
		sc.useDelimiter("}\\s*,");

		// Lista are mai multe obiecte
		// Un obiect are mai multe intrari
		// O intrare are o pereche cheie-valoare
		
		// Declarari si initializari
		String obj;
		String[] entries;
		String key = "";
		String value;
		Pattern patternKey = Pattern.compile("\"([^\"\\(\\)]+)\"");
		StringBuilder sb = new StringBuilder(900000);

		boolean first = true;
		while (sc.hasNext()) {
			obj = sc.next();
			entries = obj.split("\\s*,\\s*");
			if (first) {
				for (String entry : entries) {
					String[] entry2 = entry.split("\\s*:\\s*");
					try {
						Matcher matcher = patternKey.matcher(entry2[0]);
						matcher.find();

						key = matcher.group(0).replaceAll("\"", "");
					} catch (IllegalStateException e) {}
					//System.out.print(key + ",");
					sb.append(key + ",");
				}
				first = false;
			}
			//System.out.println();
			sb.append("\n");
			for (String entry : entries) {
				value = entry.substring(entry.indexOf(':') + 1);
				value = value.replaceAll("[\"}\\]]", "").trim();
				//System.out.print(value + ",");
				sb.append(value + ",");
			}
		}

		PrintWriter writer = new PrintWriter("convertor.out");
		writer.write(sb.toString());
		writer.close();

		sc.close();
	}
}