Skip to content

Instantly share code, notes, and snippets.

@PRotondo
Created February 15, 2013 21:11
Show Gist options
  • Save PRotondo/4963579 to your computer and use it in GitHub Desktop.
Save PRotondo/4963579 to your computer and use it in GitHub Desktop.
My solutions to the problems from Facebook Hackercup 2013 Qualification Round.
$dp = nil
def try(l,r,s)
case s[l]
when '('
((l+1)..r).each do |i|
return true if s[i] == ')' && go(l+1,i-1,s) && go(i+1,r,s)
end
false
when ':'
go(l+1,r,s) || ( l < r && (s[l+1] == '(' || s[l+1] == ')') && go(l+2,r,s))
when ')'
false
else # when 'a'..'z', ' '
go(l+1,r,s)
end
end
def go(l,r,s)
return true if l > r
if $dp[l][r] == nil
$dp[l][r] = try(l,r,s)
else
$dp[l][r]
end
end
gets.to_i.times do |i|
s = gets.strip
n = s.length
$dp = Array.new(n).map!{Array.new(n)}
out = go(0,n-1,s) ? "YES" : "NO"
puts "Case \##{i+1}: #{out}"
end
gets.to_i.times do |i|
s = gets.strip.downcase
out = ('a'..'z').map{|c| s.count(c)}.sort.zip(1..26).map{|x,y| x*y}.inject(:+)
puts "Case \##{i+1}: #{out}"
end
#include <iostream>
#include <set>
#include <vector>
#include <map>
using namespace std;
int main(void)
{
int T;
cin >> T;
for (int C = 1; C <= T; C++)
{
int n, k;
cin >> n >> k;
long long A,b,c,r;
cin >> A >> b >> c >> r;
vector <int> a(2*k+2);
set <int> S;
map <int,int> R;
S.insert(0);
a[0] = (int)A;
for (int i = 1; i < k; i++)
a[i] = (int)(((( b * ((long long)a[i-1])) % r) + (long long)c) % r), S.insert(i);
S.insert(k);
for (int i = 0; i < k; i++)
S.erase(a[i]), R[a[i]]++;
int m = a[k-1], it;
for (it = k; it < n && it < 2 * k + 1; it++)
{
m = *(S.begin());
R[m]++;
if ( (--R[a[it-k]]) == 0)
S.insert(a[it-k]);
S.erase(m);
a[ it] = m;
}
cout << "Case #" << C << ": " << a[(n-1-k)%(k+1) + k ] << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment