Skip to content

Instantly share code, notes, and snippets.

@so77id
Created September 8, 2021 17:06
Show Gist options
  • Save so77id/02cadebdf6c265884485ee13a7e3e74f to your computer and use it in GitHub Desktop.
Save so77id/02cadebdf6c265884485ee13a7e3e74f to your computer and use it in GitHub Desktop.
Evaluacion I Intento I Semestre II
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'decodeString' function below.
*
* The function is expected to return a STRING.
* The function accepts STRING s as parameter.
*/
public static String decodeString(String s) {
Stack<Integer> countStack = new Stack<>();
Stack<StringBuilder> stringStack = new Stack<>();
StringBuilder currentString = new StringBuilder();
int k = 0;
for (char ch : s.toCharArray()) {
if (Character.isDigit(ch)) {
k = k * 10 + ch - '0';
} else if (ch == '[') {
// push the number k to countStack
countStack.push(k);
// push the currentString to stringStack
stringStack.push(currentString);
// reset currentString and k
currentString = new StringBuilder();
k = 0;
} else if (ch == ']') {
StringBuilder decodedString = stringStack.pop();
// decode currentK[currentString] by appending currentString k times
for (int currentK = countStack.pop(); currentK > 0; currentK--) {
decodedString.append(currentString);
}
currentString = decodedString;
} else {
currentString.append(ch);
}
}
return currentString.toString();
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out));
String s = bufferedReader.readLine();
String res = Result.decodeString(s);
bufferedWriter.write(res);
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'findAllMirrorNumbers' function below.
*
* The function is expected to return a STRING_ARRAY.
* The function accepts INTEGER n as parameter.
*/
public static ArrayList<String> helper(int n, int j, Map<Integer, Integer> comb, ArrayList<Integer> pos, ArrayList<Integer> baseCase) {
ArrayList<String> sol = new ArrayList<String>();
if (n == 0)
return new ArrayList<String>(
Arrays.asList("")
);
if (n == 1){
for(int bc: baseCase) {
sol.add(String.valueOf(bc));
}
}
else {
ArrayList<String> ret = helper(n-2, j, comb, pos, baseCase);
for(int i = 0; i < 5; i++) {
for(String r: ret) {
if(j == n && i == 0) continue;
sol.add(String.valueOf(pos.get(i)) + r + String.valueOf(comb.get(pos.get(i))));
}
}
}
return sol;
}
public static ArrayList<String> findAllMirrorNumbers(int n) {
Map<Integer, Integer> comb = new HashMap<Integer, Integer>() {{
put(0, 0);
put(1, 1);
put(6, 9);
put(8, 8);
put(9, 6);
}};
ArrayList<Integer> pos = new ArrayList(
Arrays.asList(0,1,6,8,9)
);
ArrayList<Integer> baseCase = new ArrayList(
Arrays.asList(0,1,8)
);
return helper(n, n, comb, pos, baseCase);
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(bufferedReader.readLine().trim());
ArrayList<String> res = Result.findAllMirrorNumbers(n);
Collections.sort(res);
bufferedWriter.write(
res.stream()
.collect(joining(" "))
+ "\n"
);
bufferedReader.close();
bufferedWriter.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment