Cod sursa(job #1350879)

Utilizator rrprom13Radulescu Radu Paul - 323CC rrprom13 Data 20 februarie 2015 23:59:09
Problema Convertor Scor 40
Compilator java Status done
Runda rosedu_cdl_2015 Marime 4.83 kb
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@SuppressWarnings({ "rawtypes", "unchecked" })
public class Main {
	
	static Hashtable hash = new Hashtable();
	static int ok = 0;
	static Scanner br;
	static PrintWriter pw;
	
	void key () {
		
	}

	void value () {
		
	}
	
	void key_and_value () {
		
	}
	
	public static void main(String[] args) throws IOException {
		
//		String dir = System.getProperty("user.dir");
		br = new Scanner(new FileInputStream("convertor.in"));
		pw = new PrintWriter("convertor.out");
		String s, aux;
		int index, index2, current = 0;
		Vector val = new Vector();
		
		while (br.hasNext()) {
			s = br.nextLine();
			String[] t = s.split(",");
			for (int i = 0 ; i < t.length ; i++) {
				//	System.out.println(t[i]);
					if (t[i].matches(".*[a-zA-Z0-9]+.*")) {
					//	System.out.println(t[i] + " " + ok);
						String[] tt = t[i].split(":");
						if (tt.length == 1) {
							System.out.println(tt[0] + " " + current + " " + ok);
							if (ok == 1 && current % (val.size() * 2) == 0)
								pw.println();
							if (ok == 0 && current % 2 == 0) {
								index = t[i].indexOf("\"");
								index2 = t[i].indexOf("\"", index + 1);
								aux = t[i].substring(index + 1, index2);
								pw.print(aux + ",");
								current++;
							}
							if (ok == 0 && current % 2 == 1) {
								aux = tt[0];
								if (tt[0].contains("\"")) {									
									index = aux.indexOf("\"");
									index2 = aux.indexOf("\"", index + 1);
									aux = aux.substring(index + 1, index2);
									val.add(aux);
								}
								else {
									Matcher m = Pattern.compile("[^0-9]*([0-9]+).*").matcher(aux);
									if (m.matches()) {
									//    System.out.println(m.group(1) + "da");
										aux = aux.trim();
									    val.add(aux);
									}
								}
								current++;
							}
							if (ok == 1 && current % 2 == 1) {
								aux = tt[0];
								if (tt[0].contains("\"")) {									
									index = aux.indexOf("\"");
									index2 = aux.indexOf("\"", index + 1);
									aux = aux.substring(index + 1, index2);
									pw.print(aux + ",");
								}
								else {
									Matcher m = Pattern.compile("[^0-9]*([0-9]+).*").matcher(aux);
									if (m.matches()) {
									//    System.out.println(m.group(1) + "da");
										aux = aux.trim();
										pw.print(aux + ",");
									}
								}
								current++;
							}
							if (ok == 0 && tt[0].contains("}")) {
								ok = 1;
								pw.println();
								for (int j = 0 ; j < val.size() ; j++)
									pw.print(val.get(j) + ",");
							}
						}
						else {
							if (tt[0].matches(".*[a-zA-Z0-9]+.*")) {
								System.out.println(tt[0] + " " + current + " " + ok);
								if (ok == 0 && current % 2 == 0) {
									index = tt[0].indexOf("\"");
									index2 = tt[0].indexOf("\"", index + 1);
									aux = tt[0].substring(index + 1, index2);
									pw.print(aux + ",");
								}
								if (ok == 1 && current % (val.size() * 2) == 0)
									pw.println();
								current++;								
							}
							if (tt[1].matches(".*[a-zA-Z0-9]+.*")) {
								System.out.println(tt[1] + " " + current + " " + ok);
								if (ok == 0 && current % 2 == 1) {
									aux = tt[1];
									if (tt[1].contains("\"")) {									
										index = aux.indexOf("\"");
										index2 = aux.indexOf("\"", index + 1);
										aux = aux.substring(index + 1, index2);
										val.add(aux);
									}
									else {
										Matcher m = Pattern.compile("[^0-9]*([0-9]+).*").matcher(aux);
										if (m.matches()) {
										//    System.out.println(m.group(1) + "da");
											aux = aux.trim();
										    val.add(aux);
										}
									}
									current++;
								}
								if (ok == 1 && current % 2 == 1) {
									aux = tt[1];
									if (tt[1].contains("\"")) {									
										index = aux.indexOf("\"");
										index2 = aux.indexOf("\"", index + 1);
										aux = aux.substring(index + 1, index2);
										pw.print(aux + ",");
									}
									else {
										Matcher m = Pattern.compile("[^0-9]*([0-9]+).*").matcher(aux);
										if (m.matches()) {
										//    System.out.println(m.group(1) + "da");
											aux = aux.trim();
											pw.print(aux + ",");
										}
									}
									current++;
								}
								if (ok == 0 && tt[1].contains("}")) {
									ok = 1;
									pw.println();
									for (int j = 0 ; j < val.size() ; j++)
										pw.print(val.get(j) + ",");
								}
							}
						}
					}
					else {
						if (ok == 0 && t[i].contains("}")) {
							ok = 1;
							pw.println();
							for (int j = 0 ; j < val.size() ; j++)
								pw.print(val.get(j) + ",");
						}
					}
					
			}
		}
		br.close();
		pw.close();

	}

}