Last active
April 3, 2019 18:32
-
-
Save omidekz/22defed0299b1976dc475b0ca6136ced to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package my.ACm.Quera._26_sathe_2; | |
import java.util.HashMap; | |
import java.util.Scanner; | |
public class HaltProblem { | |
public static final int mod = 10*10*10*10*10*10*10*10*10 + 7; | |
private static String cmdOUT = ""; | |
private static HashMap<String, Integer> vars = new HashMap<>(); | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
int n = sc.nextInt(); | |
String[] lines = new String[n]; | |
sc.nextLine(); // cleare buffer | |
boolean linesNumber[] = new boolean[n]; | |
for (int i = 0; i < n; i++) | |
lines[i] = sc.nextLine(); | |
int pointer = 0; | |
boolean flag = false; | |
while (pointer < n) { | |
if (linesNumber[pointer]) { | |
System.out.println(-1); | |
System.exit(1); | |
} | |
linesNumber[pointer] = true; | |
String[] nowLine = lines[pointer].split(" "); | |
if (nowLine.length == 2) { | |
if (nowLine[0].equals("cout")) { | |
cout(nowLine[1]); | |
} else { | |
pointer = goTo(nowLine[1]); | |
flag = true; | |
} | |
} else { | |
assign(nowLine); | |
} | |
if (!flag) | |
pointer++; | |
flag = false; | |
} | |
System.out.println(cmdOUT.trim()); | |
} | |
private static void assign(String[] nowLine) { | |
// 1 , 3 , 5 | |
int a = vars.containsKey(nowLine[3]) ? vars.get(nowLine[3]) : Integer.valueOf(nowLine[3]), b = vars.containsKey(nowLine[5]) ? vars.get(nowLine[5]) : Integer.valueOf(nowLine[5]); | |
int sum = a + b; | |
vars.put(nowLine[1], sum % mod); | |
} | |
private static int goTo(String key) { | |
int pointer; | |
if (vars.containsKey(key)) { | |
pointer = Integer.valueOf(vars.get(key)); | |
} else { | |
pointer = Integer.valueOf(key); | |
} | |
return pointer - 1; | |
} | |
private static void cout(String key) { | |
if (vars.containsKey(key)) | |
cmdOUT += (vars.get(key)) + "\n"; | |
else | |
cmdOUT += ((key)) + "\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment