Problem Solving

[Topcoder] SRM211 ~ 222 연습

끄적끄적 2008. 9. 8. 20:16

SRM211 DIV2

class grafixClick
{
public:

 vector <int> checkSaveButton(vector <int> r, vector <int> c)
 {    
  VI ret;
  int n = r.size();
  REP(i, n)
   if( r[i] > 19 && r[i] < 40 && c[i] > 49 && c[i] < 100)
    ret.pb(i);
 
  return ret;
 }
};

SRM212 DIV2

class YahtzeeScore
{
public:

 int maxPoints(vector <int> toss)
 {  
  int ret = 0;

  for(int i =1; i<7; i++)
  {
   int sum = 0;
   REP(j, toss.size())
    if(toss[j] == i) sum += i;

   ret = max(ret, sum);
  }
  return ret;
 }
};

SRM213 DIV2

class Chivalry
{
public:

 string getOrder(string f, string s)
 {  
  string ret;
  int i=0, j=0;
  while( i < f.size() && j < s.size())
  {
   if(f[i] == 'M' && s[j] == 'M') ret += f[i++];    
   else if( f[i] == 'M' && s[j] == 'W') ret += s[j++];
   else ret += f[i++];
  }
  if( i < f.size())
     while( i<f.size()) ret += f[i++];
  else if( j < s.size())
     while( j<s.size()) ret += s[j++];

  return ret;
 }
};

SRM214 DIV2

class bloggoShortcuts
{
public:

 string expand(string t)
 {  
  string ret;
  int i = 0;
  int check1 = 0, check2 = 0;
  while( i< t.size())
  {
   if( t[i] == '_' && check1 == 0)
   {
    ret += "<i>";
    check1 = 1;
   }
   else if( t[i] == '_' && check1 == 1)
   {
    ret += "</i>";
    check1 = 0;
   }
   else if(t[i] == '*' && check2 == 0)
   {
    ret += "<b>";
    check2 = 1;
   }
   else if( t[i] == '*' && check2 == 1)
   {
    ret += "</b>";
    check2 = 0;
   }
   else
    ret += t[i];
   i++;

  }

  return ret;
 }
};

SRM215 DIV2
: 간만에 list를 써봤는데.. 역시 topcoder에선 vector가 편한거 같다;;

class DivisorDigits
{
public:

 string toStr(int num)
 {
  stringstream s;
  s << num;
  return s.str();
 }
 
 int howMany(int num)
 {  
  string n1 = toStr(num);
  int n = n1.size();

  list<int> val;
  REP(i, n)
   val.pb(n1[i]-'0');

  int ret = 0;
  for(list<int>::iterator pos = val.begin(); pos != val.end(); pos++)
  {
   if(*pos == 0) continue;
   if( num % (*pos) == 0) ret++;
  }

  return ret;
 }
};

SRM216 DIV2

class CultureShock
{
public:

 string translate(string t)
 {
  int n = t.size();
  if(n < 3) return t;

  for(int i=2; i<n; i++)
  {
   if( (i-3 < 0 || t[i-3] == ' ') && t[i-2] == 'Z' && t[i-1] == 'E' && t[i] == 'E'
    && (i+1 == n || t[i+1] == ' '))
   {
    t[i] = 'D';
   }
  }

  return t;
 }
};

SRM217 DIV2

class FuelConsumption
{
public:

 double maximalDistance(vector <int> v, vector <int> c, int f)
 {
  int n = v.size();
  double high = 0;
  REP(i,n)
   high = max(high,  1.0 * v[i] / c[i]);

  return high * f ;
 }
};

SRM218 DIV2

class AccessLevel
{
public:
 string canAccess(vector <int> r, int m)
 {
  string ret;
  int n = r.size();
  REP(i,n)
  {
   if( r[i] >= m) ret += 'A';
   else ret += 'D';
  }
  return ret ;
 }
};

SRM219 DIV2
: tip을 계산으로 구해보려고 하다 시간이 많이 걸렸다.
구하는 값의 연산이 flood 로 되다보니, 딱 맞는 값을 구하는게 어려웠다. 앞으로 동일한 문제가 있으면 그냥 전체 돌리면서 구할것.

class WaiterTipping
{
public:

 int maxPercent(int t, int tax, int m)
 {
  int tip;

  for(tip = 0; t + t*tax/100 + t*tip/100 <= m; tip++);
  return tip-1;
 }
};

SRM220 DIV2

class LeapAge
{
public:

 int getAge(int y, int b)
 {
  int cnt = 0;
  for(int i=b+1; i<= y; i++)
  {
   if( i % 4 == 0 && ( i % 400 ==0 || i % 100 != 0))
    cnt++;
  }
  return cnt;
 
 }
};

SRM221 DIV2
1. substr에서 두번째 인자를 안주면 npos까지 넣는다.
2. substr에서 첫번째 인자가 npos(s.end())가 와도 에러가 발생하지 않고, 빈 스트링을 반환
  : 따라서, if(i>0) y = s.substr(n-i, i); 를 y = s.substr(n-i); 로 해줬어도 됨

class EqualSubstrings
{
public:
 vector <string> getSubstrings(string s)
 {
  vector<string> ret;
  int n = s.size();
  string x, y;

  for(int i =0; i<=n; i++)
  {
   x = s.substr(0, n-i);
   if(i>0) y = s.substr(n-i, i);
   if( count(ALL(x), 'a') == count(ALL(y), 'b')) break;
  }
  ret.pb(x);
  ret.pb(y);

  return ret;
 }
};

SRM222 DIV2
: 간만에 좀 까다로운 소스? ; remain.find 를 사용할 생각을 빨리 할 수 있도록 연습필요.

class TextCompressor
{
public:
 string longestRepeat(string s)
 {
  string sub, remain, ret;
  int n = s.size();
 
  for(int cnt=1; cnt <=n/2 ; cnt++)
  {
   for(int i=0; i<n; i++)
   {
    if(i+cnt > n) break;
    sub = s.substr(i,cnt);
    remain = s.substr(i+cnt);
   
    if(remain.find(sub, 0) != string::npos)
    {
     ret = sub;
     break;
    }
   }
  }

  return ret;
 }
};

반응형