[풀이 - ACM ICPC Team]
더보기
int CalcKnown(string s1, string s2, int count)
{
int result = 0;
//
for(int i = 0; i < count; ++i)
{
if(s1[i] == '1' || s2[i] == '1')
++result;
}
//
return result;
}
vector<int> acmTeam(vector<string> topic)
{
vector<int> result;
map<int, int> m;
int attendeeCount = topic.size();
int topicCount = topic[0].size();
// make pair & calc Value
for(int i = 0; i < attendeeCount; ++i)
{
for(int j = i+1; j < attendeeCount; ++j)
{
int value = CalcKnown(topic[i], topic[j], topicCount);
m[value]++;
}
}
//
auto iter = m.rbegin();
result.push_back(iter->first); // maxCount
result.push_back(iter->second); // teamCountOfMax
return result;
}
[풀이 - Taum and B'day]
더보기
long taumBday(long b, long w, long bc, long wc, long z)
{
long result = 0;
//
long totalCount = b + w;
long d = abs(bc - wc);
// what is cheap?
long lowPrice = 0, lowCount = 0;
bc > wc ?
(lowPrice = wc, lowCount = w) :
(lowPrice = bc, lowCount = b);
// Change is effective?
if(d <= z) // Yes
result = b * bc + w * wc;
else // No ( buy at low price and pay 'change pee')
result = lowPrice * totalCount + (totalCount - lowCount) * z;
//
return result;
}
[풀이 - Organizing Containers of Balls]
더보기
string organizingContainers(vector<vector<int>> container)
{
string result = "";
//
int n = container.size();
vector<int> containerSize(n, 0);
vector<int> ballCount(n, 0);
//
for(int j = 0; j < n; ++j)
for(int i = 0; i < n; ++i)
{
containerSize[j] += container[j][i];
ballCount[i] += container[j][i];
}
//
sort(containerSize.begin(), containerSize.end());
sort(ballCount.begin(), ballCount.end());
//
for(int i = 0; i < n; ++i)
if(containerSize[i] != ballCount[i])
return "Impossible";
//
return "Possible";
}
1. [각 컨테이너가 가지고 있는 원소의 합]과 [Type별 Ball 갯수의 총합]을 각각 배열에 저장
2. 두 배열을 정렬해서 전부 일치하면 "Possible"
# Each container contains only balls of the same type.
위 문장을 컨테이너 번호와 Ball Type 번호가 일치해야 한다고 해석해서 문제가 안풀렸었다..
컨테이너 번호와 Ball Type 번호가 일치하지 않아도 가능..
[풀이 - Encryption ]
더보기
string encryption(string s)
{
string result = "";
string temp = s;
// Remove Space(' ')
remove(temp.begin(), temp.end(), ' ');
//
int L = temp.size();
int f = floor(sqrt(L));
int c = ceil(sqrt(L));
if(f * c < L)
f++;
// cut string by column
vector<string> v(f, "");
int i = -1, j = 0;
while(j < L)
{
if(j % c == 0)
++i;
//
if(j < L)
v[i] += temp[j];
//
++j;
}
// append string to result
for(int i = 0 ; i < c; ++i)
{
for(int j = 0 ; j < f; ++j)
{
if(i < v[j].size())
result += v[j][i];
}
result += " ";
}
//
return result;
}