Problem Solving

[Topcoder] SRM223 ~ 231 연습

끄적끄적 2008. 9. 10. 11:51

SRM223 DIV2

class MostProfitable
{
public:
 string bestItem(vector <int> c, vector <int> p, vector <int> s, vector <string> items)
 {
  string ret;
  int m = 0;
  REP(i, c.size())
  {
   if( m < (p[i] - c[i]) * s[i])
   {
    m = (p[i] - c[i]) * s[i];
    ret = items[i];
   }
  }
  return ret;
 }
};

SRM224 DIV2
: 문제에 char가 짝수개라고 제시되어 있는 걸 봤다면 홀수개일때 고민이 필요없었을 문제..

class InsideOut
{
public:
 string unscramble(string l)
 {
  string ret;
  int n = l.size();
  int h = n/2;
  for(int i=0; i< h; i++)
   ret += l[h-1-i];
  for(int i= 0; n-1-i >= h; i++)
   ret += l[n-1-i];

  return ret;
 }
};

SRM225 DIV2
: string거꾸러 붙일때는 그냥 reverse(ALL(s))를 호출하는게 빠르다.

class SignatureDecorator
{
public:
 string applyDecoration(string n, vector <string> c, vector <string> d)
 {
  REP(i, c.size())
  {
   if(c[i] == "prepend") n = d[i] + n;
   else if(c[i] == "append") n += d[i];
   else if(c[i] == "surround")
   {
    n = d[i] + n;
    for(int j=0; j<d[i].size(); j++)
     n += d[i][d[i].size()-1-j];
   }
  }
  return n;
 }
};

SRM226 DIV2

class VLNString
{
public:
 string makeAcronym(string longName)
 {
  stringstream in(longName);
  string ret, sub;
  while( in >> sub)
  {
   if(sub == "and"|| sub == "the"|| sub == "of") continue;
   ret += toupper(sub[0]);
  }

  return ret;
 }
};

SRM227 DIV2

class StringCompare
{
public:
 int simpleDifference(string a, string b)
 {
  int m = min(a.size(), b.size());
  int cnt = 0;
  REP(i, m)
   if(a[i] == b[i]) cnt++;

  return cnt;
 }
};

SRM228 DIV2

class ProfitCalculator
{
public:
 int percent(vector <string> items)
 {
  double a, b, c = 0, p = 0;
  REP(i, items.size())
  {
   stringstream in(items[i]);
   in >> a >> b;  
   p += a;
   c += b;
  }
  return (p-c)/p*100;
 }
};

SRM229 DIV2
: i+1 을 넣을게 아니라 그냥 if(s[i] > newscore) ++ret; 로 하는게 나을듯.
리턴할 데이터를 찾으면 굳이 break;하고 return 할게 아니라, 반복문 안에서 그냥 return을 하자..

class Highscore
{
public:
 int getRank(vector <int> s, int newscore, int p)
 {
  int ret = 1;
  if( p <= s.size() && s[p-1] >= newscore) return -1;
  s.pb(newscore);
  sort(ALL(s));
  reverse(ALL(s));

  REP(i, s.size())
   if( s[i] == newscore)
   {
    ret = i+1;
    break;
   }

  return ret;
 }
};

SRM230 DIV2
: 분모가 무조건 4이니 그냥 경우의 수를 세개로 나누는 것도 그리 나쁘지는 않음..
분모가 일정하지 않을 경우 gcd를 구해서 나누어 줄것.

class InequalityChecker
{
public:
 vector <int> getDifferences(int n)
 {
  VI ret;

  int s = 0, S = 0;
  REP(i, n-1)
   s += pow(1.0*(i+1), 3);

  REP(i, n)
   S += pow(1.0*(i+1), 3);

  int a = (S+s)*2 - pow(1.0*n, 4);
  if( a % 4 == 0)
  {
   ret.pb(a/4);
   ret.pb(1);
  }
  else if(a % 2 == 0)
  {
   ret.pb(a/2);
   ret.pb(2);
  }
  else
  {
   ret.pb(a);
   ret.pb(4);
  }

  return ret;
 }
};

SRM231 DIV2
: 앞쪽 SRM중에 동일한 문제가 있었던 듯..

class GolfScore
{
public:
 int tally(vector <int> p, vector <string> s)
 {
  int ret = 0;
  REP(i, 18)
  {
   if(s[i] == "triple bogey") ret += p[i] + 3;
   else if(s[i] == "double bogey") ret += p[i] + 2;
   else if(s[i] == "bogey") ret += p[i] + 1;
   else if(s[i] == "par") ret += p[i];
   else if(s[i] == "birdie" ) ret += p[i] -1;
   else if(s[i] ==  "eagle" ) ret += p[i] - 2;
   else if(s[i] ==  "albatross" ) ret += p[i] - 3;
   else if(s[i] ==  "hole in one" ) ret += 1;
  }
  return ret;
 }
};

반응형