Saturday, September 14, 2013

Google Code Jam Round 1C 2013 A Consonants

// Google Code Jam Round 1C 2013 A Consonants

import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;

//Google Code Jam
public class GoogleCode1 {
private static MyScanner in;
private static PrintStream out;
private static PrintStream sysout = System.out;
static int caseNum;

private static void solve() throws IOException
{
PreCompute();
int C = in.nextInt();
for (int i = 0; i < C; i++) {
caseNum = i + 1;
out.print("Case #" + caseNum + ": ");
solveCase();
}
}

private static void PreCompute() {
}

private static boolean isConsonant(char c) {
if ("aeiou".contains(Character.toString(c)))
return false;
return true;

}

private static void solveCase() throws IOException {
String s = in.nextString();
int N = in.nextInt();
int lastConsConsIdx = -1;
int nCons = 0;
long nv = 0;
for (int i = 0; i < s.length(); i++) {
if (isConsonant(s.charAt(i)))
nCons++;
else
nCons = 0;
if (nCons >= N) {
int firstCharIdx = i + 1 - N;
long nAfter = s.length() - i;
long nBefore = firstCharIdx - lastConsConsIdx;
if (firstCharIdx == 0)
nBefore = 1;
long cnt = nAfter * nBefore;
nv += cnt;
lastConsConsIdx = firstCharIdx;
}
}

out.println(nv);
}

public static void main(String[] args) throws IOException {
// helpers for input/output
boolean usingFileForIO = true;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("E:\\zin.txt");
out = new PrintStream("E:\\zout.txt");
}
else {
in = new MyScanner();
out = System.out;
}

solve();
}

// =====================================
static class MyScanner {
Scanner inp = null;

public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}

public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}

public int nextInt() throws IOException {
return inp.nextInt();
}

public long nextLong() throws IOException {
return inp.nextLong();
}

public double nextDouble() throws IOException {
return inp.nextDouble();
}

public String nextString() throws IOException {
return inp.next();
}

public String nextLine() throws IOException {
return inp.nextLine();
}
}

}

Wednesday, September 4, 2013

Codeforces Round #197 (Div. 2) B Xenia and Ringroad

// Codeforces Round #197 (Div. 2) B Xenia and Ringroad

import java.io.*;
import java.math.*;
import java.util.*;

//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
private static boolean LOCAL_TEST = false;

private static void solve() throws IOException
{
int n = in.nextInt();
int m = in.nextInt();
long time = 0;
int[] a = new int[m];
for (int i = 0; i < a.length; i++) {
a[i] = in.nextInt();
}
for (int i = 0; i < a.length; i++) {
if (i == 0)
time += a[i] - 1;
else {
if (a[i] >= a[i - 1])
time += a[i] - a[i - 1];
else
time += (n - a[i - 1]) + a[i];
}
}
out.println(time);
}

public static void main(String[] args) throws IOException {
// helpers for input/output
out = System.out;
try {
String cname = System.getenv("COMPUTERNAME");
if (!cname.equals(""))
LOCAL_TEST = true;
} catch (Exception e) {
}
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin.txt");
}
else {
boolean usingFileForIO = false;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("input.txt");
out = new PrintStream("output.txt");
}
else {
in = new MyScanner();
out = System.out;
}
}

solve();
}

// =====================================
static class MyScanner {
BufferedReader bufReader;
StringTokenizer strTok;

public MyScanner() throws IOException
{
bufReader = new BufferedReader(new InputStreamReader(System.in));
strTok = new StringTokenizer("");
}

public MyScanner(String inputFile) throws IOException {
bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(
inputFile)));
strTok = new StringTokenizer("");
}

String GetNextToken() throws IOException {
if (!strTok.hasMoreTokens())
strTok = new StringTokenizer(bufReader.readLine());
return strTok.nextToken();
}

public int nextInt() throws IOException {
return Integer.valueOf(GetNextToken());
}

public long nextLong() throws IOException {
return Long.valueOf(GetNextToken());
}

public double nextDouble() throws IOException {
return Double.valueOf(GetNextToken());
}

public String nextString() throws IOException {
return GetNextToken();
}

public String nextLine() throws IOException {
return bufReader.readLine();
}

public int countTokens() {
return strTok.countTokens();
}

public boolean hasMoreTokens() {
return strTok.hasMoreTokens();
}
}

}

Codeforces Round #197 (Div. 2) A Helpful Maths

// Codeforces Round #197 (Div. 2) A Helpful Maths

import java.io.*;
import java.math.*;
import java.util.*;

//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
private static boolean LOCAL_TEST = false;

private static void solve() throws IOException
{
String s = in.nextLine();
int[] nums = new int[4];
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '1')
nums[1]++;
else if (c == '2')
nums[2]++;
else if (c == '3')
nums[3]++;
}
int numAll = nums[1] + nums[2] + nums[3];
String r = "";
int i = 0;
for (int j = 1; j <= 3; j++) {
for (int k = 0; k < nums[j]; k++) {
r += j;
i++;
if (i < numAll)
r += "+";
}
}
out.println(r);
}

public static void main(String[] args) throws IOException {
// helpers for input/output
out = System.out;
try {
String cname = System.getenv("COMPUTERNAME");
if (!cname.equals(""))
LOCAL_TEST = true;
} catch (Exception e) {
}
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin.txt");
}
else {
boolean usingFileForIO = false;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("input.txt");
out = new PrintStream("output.txt");
}
else {
in = new MyScanner();
out = System.out;
}
}

solve();
}

// =====================================
static class MyScanner {
BufferedReader bufReader;
StringTokenizer strTok;

public MyScanner() throws IOException
{
bufReader = new BufferedReader(new InputStreamReader(System.in));
strTok = new StringTokenizer("");
}

public MyScanner(String inputFile) throws IOException {
bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(
inputFile)));
strTok = new StringTokenizer("");
}

String GetNextToken() throws IOException {
if (!strTok.hasMoreTokens())
strTok = new StringTokenizer(bufReader.readLine());
return strTok.nextToken();
}

public int nextInt() throws IOException {
return Integer.valueOf(GetNextToken());
}

public long nextLong() throws IOException {
return Long.valueOf(GetNextToken());
}

public double nextDouble() throws IOException {
return Double.valueOf(GetNextToken());
}

public String nextString() throws IOException {
return GetNextToken();
}

public String nextLine() throws IOException {
return bufReader.readLine();
}

public int countTokens() {
return strTok.countTokens();
}

public boolean hasMoreTokens() {
return strTok.hasMoreTokens();
}
}

}