Thursday, July 3, 2014

Codeforces Beta Round #54 (Div. 2), problem: (A) Chat room solution

Codeforces Beta Round #54 (Div. 2), problem: (A) Chat room: http://codeforces.com/problemset/problem/58/A

Codeforces Beta Round #54 (Div. 2), problem: (A) Chat room editorial: http://codeforces.com/blog/entry/1771

Codeforces Beta Round #54 (Div. 2), problem: (A) Chat room solution: http://ideone.com/GPHEV1


#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int main() {
    int i=0, cnt=0;
    char s[105];
    scanf("%s", s);
    for(; i<strlen(s); ++i) if(s[i]=='h') {cnt++, i++; break;}
    for(; i<strlen(s); ++i) if(s[i]=='e') {cnt++, i++; break;}
    for(; i<strlen(s); ++i) if(s[i]=='l') {cnt++, i++; break;}
    for(; i<strlen(s); ++i) if(s[i]=='l') {cnt++, i++; break;}
    for(; i<strlen(s); ++i) if(s[i]=='o') {cnt++, i++; break;}
    if(cnt>=5) printf("YES");
    else printf("NO");
    return 0;
}


Codeforces Beta Round #57 (Div. 2), problem: (A) Ultra-Fast Mathematician solution

Codeforces Beta Round #57 (Div. 2), problem: (A) Ultra-Fast Mathematician: http://codeforces.com/problemset/problem/61/A

Codeforces Beta Round #57 (Div. 2), problem: (A) Ultra-Fast Mathematician editorial: http://codeforces.com/blog/entry/1342

Codeforces Beta Round #57 (Div. 2), problem: (A) Ultra-Fast Mathematician solution: http://ideone.com/3re7sc

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

int main() {
    int c, d;
    char a[105], b[105];
    string e="";
    scanf("%s%s", a, b);
    for(int i=0; i<strlen(a); i++) {
        if(a[i]=='1') c=1;
        else c=0;
        if(b[i]=='1') d=1;
        else d=0;
        if(c^d) e+='1';
        else e+='0';
    }
    printf("%s", e.c_str()); //http://stackoverflow.com/a/6419751/2948746
    return 0;
}

for xor c++: http://en.wikipedia.org/wiki/Bitwise_operations_in_Chttp://www.cplusplus.com/doc/boolean/

for c_str() c++: http://stackoverflow.com/a/6419751/2948746


Codeforces Round #164 (Div. 2), problem: (A) Games solution

Codeforces Round #164 (Div. 2), problem: (A) Games: http://codeforces.com/problemset/problem/268/A

Codeforces Round #164 (Div. 2), problem: (A) Games editorial: http://codeforces.com/blog/entry/6545?locale=en

Codeforces Round #164 (Div. 2), problem: (A) Games solution: http://ideone.com/3fx0Ww


#include <iostream>
#include <cstdio>
using namespace std;

int main() {
 int n, a[35], h[35], cnt=0;
 scanf("%d", &n);
 for(int i=0; i<n; i++) scanf("%d%d", &a[i], &h[i]);
 for(int i=0; i<n; i++) for(int j=0; j<n; j++) if(h[i]==a[j] && i!=j) cnt++;
 printf("%d", cnt);
 return 0;
}


TopCoder High School SRM 34 Level one - 250 pt - Quote Contest solution

TopCoder High School SRM 34 Level one - 250 pt - Quote Contest statement: http://community.topcoder.com/stat?c=problem_statement&pm=7594&rd=10769

TopCoder High School SRM 34 Level one - 250 pt - Quote Contest editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm34

TopCoder High School SRM 34 Level one - 250 pt - Quote Contest solution: 


#include <iostream>
#include <vector>
#include <sstream>
using namespace std;

class QuoteContest {
    public: string bestQuote(vector <string> q) {
        int b, c, ma=0;
        string d, e;
        for(int i=0; i<q.size(); i++) {
            stringstream a(q[i]);
            a>>b>>c>>d;
            if(b+c>ma) ma=b+c, e=d;
        }
        return e;
    }
};

TopCoder High School SRM 33 Level one - 250 pt - Castle Guards solution

TopCoder High School SRM 33 Level one - 250 pt - Castle Guards statement: http://community.topcoder.com/stat?c=problem_statement&pm=8017&rd=10767

TopCoder High School SRM 33 Level one - 250 pt - Castle Guards editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm33

TopCoder High School SRM 33 Level one - 250 pt - Castle Guards solution: 

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class CastleGuards {
    public: int missing(vector <string> c) {
        int a[55]={0}, b[55]={0}, ca=0, cb=0;
        for(int i=0; i<c.size(); i++) {
            for(int j=0; j<c[i].length(); j++) {
                if(c[i][j]=='X') a[i]=1, b[j]=1;
            }
        }
        for(int i=0; i<c.size(); i++) if(a[i]==0) ca++;
        for(int i=0; i<c[0].length(); i++) if(b[i]==0) cb++;
        return max(ca, cb);
    }
};

Tuesday, July 1, 2014

TopCoder High School SRM 32 Level one - 250 pt - Boulder Race solution

TopCoder High School SRM 32 Level one - 250 pt - Boulder Race statement: http://community.topcoder.com/stat?c=problem_statement&pm=7237&rd=10656

TopCoder High School SRM 32 Level one - 250 pt - Boulder Race editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm32

TopCoder High School SRM 32 Level one - 250 pt - Boulder Race solution: 

#include <iostream>
#include <vector>
#include <map>
#include <climits>
using namespace std;

class BoulderRace {
    public: int winner(vector <string> b, int d) {
        int a, mi=INT_MAX, cnt, idx=0;
        map <int, int> c;
        for(int i=0; i<b.size(); i++) {
            a=0;
            cnt=0;
            while(a<d) {
                for(int j=0; j<b[i].length(); j++) {
                    a+=b[i][j]-48;
                    if(a>=d) {
                        if(j+b[i].length()*cnt<mi) mi=j+b[i].length()*cnt, idx=i;
                        break;
                    }
                }
                cnt++;
            }
        }
        return idx;
    }
};

TopCoder High School SRM 31 Level one - 250 pt - Traffic Report solution

TopCoder High School SRM 31 Level one - 250 pt - Traffic Report statement: http://community.topcoder.com/stat?c=problem_statement&pm=7425&rd=10655

TopCoder High School SRM 31 Level one - 250 pt - Traffic Report editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm31

TopCoder High School SRM 31 Level one - 250 pt - Traffic Report solution: 


#include <iostream>
#include <vector>
#include <sstream>
#include <map>
using namespace std;

class TrafficReport {
    public: int bestRoute(vector <string> ro, vector <string> re) {
        string c;
        int b, ans=0;
        map <string, int> a, d;
        for(int i=0; i<ro.size(); i++) { stringstream e(ro[i]); while(e>>b>>c) a[c]=b, ans+=b; }
        for(int i=0; i<re.size(); i++) {
            stringstream f(re[i]);
            while(f>>b>>c) if(a.count(c)) ans+=b;
        }       
        return ans;
    }
};

note: use count() function from <map> to find specific thing you are looking for: http://www.cplusplus.com/reference/map/map/count/

size_type count (const key_type& k) const;
Count elements with a specific key

Searches the container for elements with a key equivalent to k and returns the number of matches.