2017년 12월 23일 토요일

[C++] Topcoder FriendScore Solution

/*
I wrote this code assuming that "one way friendship" might exist in test cases.
but actually it's not in Topcoder's test cases.
*/


#include <string>
#include <vector>
#include <iostream>

using namespace std;

class FriendScore {
   
public:
    int highestScore(vector<string> friends){
        int max=0;
        int size = friends[0].length();
        for(int i=0;i<size;i++){
            int temp=0;
            vector<int> temp_arr(size,1);    // prevention of duplication
           
            for(int j=0;j<size;j++){
                if(i == j) continue;
                if(friends[i][j] == 'Y'){
               
                    if(friends[j][i] == 'Y'){       // checking whether it's one way friendship
                        if(temp_arr[j]){
                        temp++;
                        temp_arr[j]=0;                       
                        }

                    }

                    for(int k=0;k<size;k++){
                        if(k==i) continue;
                        if(k==j) continue;

                        if(friends[k][j] == 'Y'){
                            if(temp_arr[k]){
                                temp++;
                                temp_arr[k]=0;                       
                            }

                        }
                    }
                }   
            }
           
            cout << "temp=" << temp;
            if(temp > max) max = temp;
        }
       
        return max;
    }
};

댓글 없음:

댓글 쓰기