Thursday, March 24, 2016

HackerRank: Two string - thinking in C++ over 15 ways

March 24, 2016

Julia likes to get idea how to write modern C++, she started to read at least 50 C++ solutions a week.

Always look for brilliant ideas to solve a simple problem. Take time to read other people's code, and then, find really excellent ideas.

Algorithm problem solving -> More ideas -> Good thinker -> Confidence.

Julia, think in C++ - solve problems using the following:
1. bit operation
2. array int[26]
3. class set
4. class unordered_set, functions: reserve, insert, count
5. class bitset, size_t
5. cin vs gets, pointer - some calculation

Problem statement is here.


Solution 1:
Here it is, namespace std, >>, string class, set<char>, auto, &x, for(auto &x: A), modern C++ style

C++ code is here


Solution 2: using bit operation - beautiful code, genius!
source code reference:

https://problemsolvingnotes.wordpress.com/page/2/
https://www.hackerrank.com/MarioYC
ACM ICPC World Finalist 2012, 2013
count - reference:
http://www.cplusplus.com/reference/algorithm/count/

sync with stdio
http://www.cplusplus.com/reference/ios/ios_base/sync_with_stdio/

using | inclusive OR
http://www.cplusplus.com/doc/tutorial/operators/
solution in C++:
https://gist.github.com/jianminchen/a0724f1fbbe5c621cd63

using namespace std;

int main(){
    ios::sync_with_stdio(0);

    string A,B;

    int T;

    cin >> T;

    int cont[26];

    while(T--){
        cin >> A >> B;    
   
        memset(cont,0,sizeof cont);
   
        for(int i = 0;i < A.size();++i)
            cont[ A[i] - 'a'] |= 1;   // Julia's comment: set first bit as 1, using | - inclusive OR
   
        for(int i = 0;i < B.size();++i)
            cont[ B[i] - 'a'] |= 2;  //  Julia's comment: set second bit as 1, using | - inclusive OR
   
        if(count(cont,cont + 26,3) > 0) cout << "YES\n";
// check array cont - if there is any number is 3, if found, return true
        else cout << "NO\n";
    }

    return 0;
}

Solution 3:
https://gist.github.com/jianminchen/6b443714e61f1c8c1382

Solution 4:
https://gist.github.com/jianminchen/dcfc885d390939a08ae2

Solution 5:
https://gist.github.com/jianminchen/13c2d3832f1cbd53433c

Solution 6: set class, insert, find, end functions
read set class end function:
http://www.cplusplus.com/reference/set/set/find/
set class insert function:
http://www.cplusplus.com/reference/set/set/insert/

https://gist.github.com/jianminchen/1fd8c8e9acdc72f18d10

Solution 7:
https://gist.github.com/jianminchen/f4b6dcea9936347192e9

Solution 8: unordered_set class, reserved function, insert, count function
unordered_set class reserve function:  http://www.cplusplus.com/reference/unordered_set/unordered_set/reserve/

https://gist.github.com/jianminchen/a3865ec039c092476492

Solution 9: cin vs gets, scanf, pointer - calculation
warm up with some concepts:
C++ gets:
http://www.cplusplus.com/reference/cstdio/gets/

compare two solutions - difference
https://gist.github.com/jianminchen/f35171b0c2ddcd403c2b

code is better! - https://www.hackerrank.com/avolchek 24 Gold
https://gist.github.com/jianminchen/d9c0d7a25ca2e24ca17b

Solution 11:
warm up with std::fill function template:
http://www.cplusplus.com/reference/algorithm/fill/
very strong C++ coder:
https://www.hackerrank.com/Informatimukas
https://gist.github.com/jianminchen/ff3fd27b8c1245322adf

Solution 12: bitset, size_t
https://gist.github.com/jianminchen/765ba74888057b887cac

Solution 13: vector class template
https://gist.github.com/jianminchen/c0e3a44edc31b3804331

Solution 14: 
very readable - two functions
https://gist.github.com/jianminchen/43f23e82ee04076c7496

No comments:

Post a Comment