Skip to content

Instantly share code, notes, and snippets.

@MLLeKander
Last active December 11, 2015 22:38
Show Gist options
  • Save MLLeKander/4670418 to your computer and use it in GitHub Desktop.
Save MLLeKander/4670418 to your computer and use it in GitHub Desktop.
import java.util.*;
class BeautifulStrings {
public static String solve(Scanner scan) {
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
for (char c : scan.nextLine().toLowerCase().toCharArray())
if (Character.isLowerCase(c))
map.put(c, (map.containsKey(c) ? map.get(c) : 0)+1);
List<Integer> values = new ArrayList<Integer>(map.values());
Collections.sort(values, Collections.reverseOrder());
int total = 0, c = 26;
for (int i : values)
total += i * c--;
return total+"";
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int lines = scan.nextInt();
scan.nextLine();
for (int i = 1; i <= lines; i++) {
System.out.println("Case #"+i+": "+solve(scan));
}
}
}
import java.util.*;
class Smileys {
static class State implements Comparable<State> {
short pos, value;
public State(short p, short v) {pos = p; value = v;}
public int compareTo(State o) {
if (pos != o.pos)
return pos - o.pos;
return o.value - value;
}
public boolean equals(Object o) {
if (!(o instanceof State))
return false;
State s = (State)o;
return s.pos == pos && s.value == value;
}
public String toString() {return "("+pos+", "+value+")";}
}
public static String solve(Scanner scan) {
int depth = 0;
char[] line = scan.nextLine().replaceAll("[^:()]+", "o").toCharArray();
Set<State> visited = new TreeSet<State>();
PriorityQueue<State> queue = new PriorityQueue<State>();
queue.add(new State((short)0,(short)0));
while (queue.size() > 0) {
State s = queue.poll();
if (s.value < 0 || visited.contains(s) || (s.pos >= line.length && s.value != 0))
continue;
if (s.value == 0 && s.pos == line.length)
return "YES";
visited.add(new State(s.pos, s.value));
if (line[s.pos] == '(')
s.value++;
else if (line[s.pos] == ')')
s.value--;
else if (line[s.pos] == ':' && (line[s.pos+1] == '(' || line[s.pos+1] == ')'))
queue.add(new State((short)(s.pos+2), s.value));
s.pos++;
queue.add(s);
}
return "NO";
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int lines = scan.nextInt();
scan.nextLine();
for (int i = 1; i <= lines; i++) {
System.out.println("Case #"+i+": "+solve(scan));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment