Skip to content

Instantly share code, notes, and snippets.

@so77id
Created May 5, 2020 22:28
Show Gist options
  • Save so77id/0b635fa5713aa2c257c72b423daf0d50 to your computer and use it in GitHub Desktop.
Save so77id/0b635fa5713aa2c257c72b423daf0d50 to your computer and use it in GitHub Desktop.
Primera solemen eddya
import java.io.*;
import java.util.*;
public class CopiarPegarBorrarDeshacer {
static public int append(Stack<Character> stack, String buff) {
int count = 0;
for(char c:buff.toCharArray()){
stack.push(c);
count++;
}
return count;
}
static public String deletek(Stack<Character> stack, int k){
String tmp = "";
for(int j=0;j<k;j++){
tmp += String.valueOf(stack.pop());
}
StringBuilder pmt = new StringBuilder();
pmt.append(tmp);
return new String(pmt.reverse());
}
static public void print(Stack<Character> stack, int k, int count) {
Stack<Character> tmp = new Stack<Character>();
int diff = count-k;
for(int i=0;i<diff;i++){
tmp.push(stack.pop());
}
System.out.println(stack.peek());
while(!tmp.isEmpty()) {
stack.push(tmp.pop());
}
}
public static class Undo {
int op;
int k;
String str;
public Undo(int op, int k) {
this.op = op;
this.k = k;
this.str = "";
}
public Undo(int op, String str) {
this.op = op;
this.str = str;
this.k = 0;
}
}
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
int q = stdin.nextInt();
int op, k;
int count, globalCount=0;
String buff;
Undo tmp;
Stack<Character> stack = new Stack<Character>();
Stack<Undo> undoStack = new Stack<Undo>();
for(int i=0;i<q;i++){
op = stdin.nextInt();
switch(op) {
case 1:
buff = stdin.next();
count = append(stack, buff);
undoStack.push(new Undo(2, count));
globalCount += count;
break;
case 2:
k = stdin.nextInt();
buff = deletek(stack, k);
globalCount -=k;
undoStack.push(new Undo(1, buff));
break;
case 3:
k = stdin.nextInt();
print(stack, k, globalCount);
break;
case 4:
if(!undoStack.isEmpty()) {
tmp = undoStack.pop();
if(tmp.op == 1) {
count = append(stack, tmp.str);
globalCount += count;
} else if (tmp.op == 2){
buff = deletek(stack, tmp.k);
globalCount -=tmp.k;
}
}
break;
}
}
}
}
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class TodoEnOrden {
static class Node {
public int data;
public Node left;
public Node right;
public Node(int data, Node left, Node right) {
this.data = data;
this.left = left;
this.right = right;
}
}
static class Tree{
public Node root;
public Tree(int[] data){
this.root = this.extractTree(data, 0, data.length-1);
}
private Node extractTree(int[] data, int min, int max) {
if(min > max) return null;
if(min == max) return new Node(data[min], null, null);
int mid = (min + max)/2;
return new Node(data[mid], extractTree(data, min, mid-1), extractTree(data, mid+1, max));
}
public boolean checkBST() {
return this.checkBST(this.root);
}
private boolean check(Node root, int min, int max) {
if (root != null) {
if (root.data >= max || root.data <= min) {
return false;
}
else {
return check(root.left, min, root.data) & check(root.right, root.data, max);
}
}
else {
return true;
}
}
private boolean checkBST(Node root) {
return check(root, 0, Integer.MAX_VALUE);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] data = new int[n];
for(int i=0;i<n;i++){
data[i] = scanner.nextInt();
}
Tree newTree = new Tree(data);
if(newTree.checkBST() == true) {
System.out.println("Yes");
} else {
System.out.println("No");
}
scanner.close();
}
}
import java.util.*;
import java.lang.*;
public class UdpBank
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner stdin = new Scanner(System.in);
int n;
String key, buff;
Map<String, Integer> list_map;
Vector<String> list;
n = stdin.nextInt();
list_map = new TreeMap<String, Integer>();
list = new Vector<String>();
for(int i=0;i<n;i++){
key = stdin.next();
key += " " + stdin.next();
key += " " + stdin.next();
key += " " + stdin.next();
key += " " + stdin.next();
key += " " + stdin.next();
if(list_map.containsKey(key)) {
list_map.put(key, list_map.get(key)+1);
} else {
list_map.put(key,1);
list.add(key);
}
}
Collections.sort(list);
for(String k:list){
System.out.println(k + " " +list_map.get(k));
}
}
}
import java.util.*;
import java.io.*;
class Node {
Node left;
Node right;
int data;
Node(int data) {
this.data = data;
left = null;
right = null;
}
}
public class UnNivelALaVez {
/*
class Node
int data;
Node left;
Node right;
*/
public static void levelOrder(Node root) {
Queue<Node> q = new LinkedList<>();
Node tmp;
if(root.left != null) q.add(root.left);
if(root.right != null) q.add(root.right);
System.out.print(root.data);
while(!q.isEmpty()) {
tmp = q.remove();
System.out.print(" " + tmp.data);
if(tmp.left != null) q.add(tmp.left);
if(tmp.right != null) q.add(tmp.right);
}
System.out.println("");
}
public static Node insert(Node root, int data) {
if(root == null) {
return new Node(data);
} else {
Node cur;
if(data <= root.data) {
cur = insert(root.left, data);
root.left = cur;
} else {
cur = insert(root.right, data);
root.right = cur;
}
return root;
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
Node root = null;
while(t-- > 0) {
int data = scan.nextInt();
root = insert(root, data);
}
scan.close();
levelOrder(root);
}
}
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class UnPasoAtras {
static class Node {
public int data;
public Node next;
public Node(int nodeData) {
this.data = nodeData;
this.next = null;
}
}
static class List {
public Node head;
public Node tail;
public List() {
this.head = null;
this.tail = null;
}
public void insertNode(int nodeData) {
Node node = new Node(nodeData);
if (this.head == null) {
this.head = node;
} else {
this.tail.next = node;
}
this.tail = node;
}
}
static void reversePrint(Node head) {
if(head == null) return;
reversePrint(head.next);
System.out.println(head.data);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List llist = new List();
int llistCount = scanner.nextInt();
for (int i = 0; i < llistCount; i++) {
int llistItem = scanner.nextInt();
llist.insertNode(llistItem);
}
reversePrint(llist.head);
scanner.close();
}
}
import java.util.*;
import java.lang.*;
public class XsiYno
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner stdin = new Scanner(System.in);
int T = stdin.nextInt();
int n,x,y;
String v, step;
v = new String("");
for(int i=0;i<T;i++) {
n = stdin.nextInt();
x = stdin.nextInt();
y = stdin.nextInt();
step = v;
for(int j=x; j<n; j+=x) {
if(j%y != 0) {
System.out.print(step + j);
if(step == v) step = " ";
}
}
System.out.print("\n");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment