Skip to content

Instantly share code, notes, and snippets.

@nebuta
Created April 5, 2014 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nebuta/9996851 to your computer and use it in GitHub Desktop.
Save nebuta/9996851 to your computer and use it in GitHub Desktop.
Google Code Jam 2009 qual round: A. Alien Language
use std::io::BufferedReader;
use std::io::stdin;
// Google Code Jam: Qualification Round 2009, Problem A. Alien Language
//https://code.google.com/codejam/contest/90101/dashboard#s=p0
// Compiles on Rust 0.11-pre
// This uses unwrap() functions that can fail.
// This is okay for programming contest problems.
fn main(){
let mut stdin = BufferedReader::new(stdin());
let line = stdin.read_line().unwrap();
let ss: ~[int] = line.trim().split(' ').map(|a|from_str(a).unwrap()).collect();
let (l,d,n) = (ss[0] as uint,ss[1] as uint,ss[2] as uint);
//println!("{}",(l,d,n));
let words: ~[~str] = range(0,d).map(|_| stdin.read_line().unwrap()).collect();
//println!("{}",words);
let cases: ~[~str] = range(0,n).map(|_| stdin.read_line().unwrap()).collect();
for (i,c) in cases.iter().enumerate() {
let cp = parse(*c);
//println!("{}",cp);
let mut count = 0;
for w in words.iter() {
let cs: ~[char] = w.chars().collect();
let mut ok = true;
for i in range(0,l as uint) {
if !cp[i].contains(&cs[i]) {
ok = false;
break;
}
}
if ok {count += 1}
}
println!("Case \\#{}: {}",i+1,count);
}
}
fn parse(s: &str) -> ~[~[char]]{
let mut r: ~[~[char]] = ~[];
let mut paren = false;
let cs: ~[char] = s.trim().chars().collect();
let len = cs.len();
let mut i = 0;
let mut j = 0;
loop {
if cs[i] == '(' {
paren = true;
r.push(~[]);
}else if cs[i] == ')' {
paren = false;
j += 1;
}else{
if paren {
r[j].push(cs[i]);
}else {
r.push(~[]);
r[j].push(cs[i]);
j += 1;
}
}
i += 1;
if i == len {
break;
}
}
r
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment