Problem Solving

[Topcoder] SRM265 ~ 277 연습

끄적끄적 2008. 9. 17. 11:41

SRM265 DIV2
: islower(char c), isupper(char c) 의 용법도 익숙해지자.

class FontSize
{
public:

 int getPixelWidth(string s, vector <int> u, vector <int> l)
 {
  int n = s.size();
  int ret = n-1;

  REP(i, n)
  {
   if( s[i] >= 'A' && s[i] <= 'Z')
    ret += u[s[i]-'A'];
   else if(s[i] >= 'a' && s[i] <= 'z')
    ret += l[s[i]-'a'];
   else if(s[i] == ' ')
    ret += 3;
  }

  return ret;
 }
};

SRM266 DIV2
: 반올림처리하느라 함수가 있나 싶어 찾다 시간소모함.. 그냥 0.5를 더한다음에 int로 형변환 하면 됨..
두번째 돌만 건너가는 경우 생각못해서 fail났음.(Notes구문을 잘 보자는 교훈..)
나중에 다시 한번 풀어볼 것.

class SwimmersDelight
{
public:

 int longestJump(vector <int> x, vector <int> y)
 {
  int c1 = max( x[0], 10-x[0]);
  int c2 = max( x[1], 10-x[1]);
  int ret = min(c1, c2);

  if(x[0] > x[1])
  {
   swap(x[0], x[1]);
   swap(y[0], y[1]);
  }

  int c3 = max(x[0], 10-x[1]);

  double s = sqrt((double)(x[1]-x[0])*(x[1]-x[0]) + (y[1]-y[0])*(y[1]-y[0]));
  int temp = s;
  if( s >= (double)temp + 0.5) temp++;
  c3 = max(c3, temp);

  ret = min(ret, c3);
  return ret;
 }
};

SRM267 DIV2

class TaxTable
{
public:

 int income(int t)
 {
  if( (t+6525.0)*((double)100/25) < 100000) return -1;
  else if( (t+6525.0)*((double)100/25) <= 117250) return (t+6525.0)*((double)100/25)+0.5;
  else if( (t+10042.5)*((double)100/28) <= 178650) return (t+10042.5)*((double)100/28)+0.5;
  else if( (t+18975.0)*((double)100/33) <= 319100) return (t+18975.0)*((double)100/33)+0.5;
  else return (t+25357.0)*((double)100/35)+0.5;
 }
};

SRM268 DIV2
: 문제 이해하는 시간을 줄이자..

class CrossWordPuzzle
{
public:

 int countWords(vector <string> b, int s)
 {
  int ret = 0;
  REP(i, b.size())
  {
   string str = b[i];
   int cnt = 0;
   REP(j, str.size())
   {
    if( str[j] == '.') cnt++;
    else
    {
     if(cnt == s) ret++;
     cnt = 0;
    }
   }
   if(cnt == s) ret++;
  }
  return ret;

 }
};

SRM269 DIV2
: substr을 쓰면 간단할 문제를 복잡하게 짠 듯 함..

class AccessChanger
{
public:

 vector <string> convert(vector <string> p)
 {
  vector<string> ret;
  REP(i, p.size())
  {
   string s = p[i];
   string r;
   int com = 0;
   for(int j=0; j<s.size(); j++)
   {
    if( j > 0 && s[j-1] == '/' && s[j] == '/')
    {
     com = 1;
     r += s[j];
    }
    else if( j > 0 && com == 0 && s[j-1] == '-' && s[j] == '>')
    {
     r.erase( r.size()-1 );
     r += '.';
    }
    else
    {     
     r += s[j];
    }
   }
   ret.pb(r);

  }

  return ret;

 }
};

substr을 이용해서 다시 짜본 소스
class AccessChanger
{
public:
 vector <string> convert(vector <string> p)
 {
  vector<string> ret;
  REP(i, p.size())
  {
   string s = p[i];
   REP(j, s.size()-1)
   {
    if( s.substr(j, 2) == "//") break;
    if( s.substr(j, 2) == "->")
     s = s.substr(0, j) + '.' + s.substr(j+2);
   }
   ret.pb(s);
  }
  return ret;
 }
};

SRM270 DIV2
: 중복된거 거르는 방법으로 vector<int> 를 set<int>로 넣는 방법도 있구나.
또는 p.erase(unique(ALL(p)), p.end()); 로 중복된 내용 지우는 방법도 있음.

class BuyingCheap
{
public:
 int thirdBestPrice(vector <int> p)
 {
  sort(ALL(p));
  VI q;
  REP(i, p.size())
   if( count(ALL(q), p[i]) == 0) q.pb(p[i]);

  if( q.size() < 3) return -1;
  return q[2];
 }
};

set을 이용해 다시 짜본 소스
class BuyingCheap
{
public:

 int thirdBestPrice(vector <int> p)
 {
  sort(ALL(p));
  set<int> t(ALL(p));
  VI q(ALL(t));

  if( q.size() < 3) return -1;
  return q[2];
 }
};

SRM271 DIV2
: 처음에는 그냥 switch문이 떠올라서 짰다. 코딩시간을 줄이려면 배열에 넣어놓고 돌리는게 빠름

class CheckFunction
{
public:

 int newFunction(string c)
 {
  int ret = 0;
  REP(i, c.size())
  {
   switch(c[i])
   {
   case '0':
    ret += 6;break;
   case '1':
    ret+= 2;break;
   case '2':
    ret+= 5;break;
   case '3':
    ret+= 5;break;
   case '4':
    ret+= 4;break;
   case '5':
    ret+= 5;break;
   case '6':
    ret+= 6;break;
   case '7':
    ret+= 3;break;
   case '8':
    ret+= 7;break;
   case '9':
    ret+= 6;break;
   }
  }
  return ret;
 }
};

배열을 이용해서 다시 짜본 소스
class CheckFunction
{
public:

 int newFunction(string c)
 {
  int ret = 0;
  int index[] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};

  REP(i, c.size())
   ret += index[c[i]-'0'];
 
  return ret;
 }
};

SRM272 DIV2

class HammingDistance
{
public:

 int minDistance(vector <string> n)
 {
  int ret = INT_MAX;

  REP(i, n.size())
  {
   REP(j, n.size())
   {
    if( i==j) continue;
    string a = n[i];
    string b = n[j];
    int cnt = 0;
    REP(k, a.size())
     if(a[k] != b[k]) cnt++;

    ret = min(ret, cnt);
   }
  }
 
  return ret;
 }
};

SRM273 DIV2

class AimToTen
{
public:

 int need(vector <int> m)
 {
  int n = m.size();

  int sum = 0;
  REP(i, n) sum+= m[i];

  int i = 0;
  while( (1.0*sum+i*10)/(n+i) < 9.5 )
  {
   i++;
  }
   
  return i;
 }
};

SRM274 DIV2

class SimpleDuplicateRemover
{
public:

 vector <int> process(vector <int> s)
 {
  VI ret;
  int n = s.size();
  REP(i, n)
   if( count(ALL(ret), s[n-1-i]) == 0 ) ret.pb(s[n-1-i]);

  reverse(ALL(ret));
  return ret;
 }
};

SRM275 DIV2

class HingedDoor
{
public:

 int numSwings(int init, int r)
 {  
  double s = init;
  if( s <= 5) return 0;
  int ret = 1;
  while(1)
  {     
   if( s / r <= 5) return ret;
   s = s / r;
   ret++;
  }
 }
};

SRM276 DIV2
: round down문제는 될 수 있으면 미리 곱한 후에 나누자.

class TestCurve
{
public:

 vector <int> determineGrades(vector <int> s)
 {
  int m = 0;
  REP(i, s.size())
   m = max(m, s[i]);

  REP(i, s.size())
   s[i] = s[i] * 100 / m;

  return s;
 }
};

SRM277 DIV2
: 변수명을 find로 썼다가 find함수가 에러나길래 왜 그런가 한참 찾았다;;
변수명 쓸때 함수명은 될 수 있으면 안쓰도록 유의해야겠음.


class SandwichBar
{
public:

 int whichOrder(vector <string> a, vector <string> o)
 {
  int n = o.size();
  REP(i, n)
  {
   stringstream in(o[i]);
   string s;
   
   int next = 0;
   while( in >> s)
   {
    if( find(ALL(a), s) == a.end() )
    {
     next = 1;
     break;
    }
   }
   if(next == 0) return i;
  }

  return -1;
 }
};


반응형