Problem Solving

[Topcoder] SRM197 ~ 210 연습

끄적끄적 2008. 9. 8. 10:15

SRM197 DIV2
: 심플한 문제.

class GardenHose
{
public:
 int countDead(int n, int r, int c, int h)
 {  
  int row_c = h / r;
  int col_c = h / c;

  row_c = max(n - (2 * row_c), 0);
  col_c = max(n - (2 * col_c), 0);

  return row_c * col_c;
 }
};

SRM198 DIV2
: 캡쳐 소스처럼 짜는게 좀더 간결하다.

사용자 삽입 이미지












class Reppity
{
public:
 int longestRep(string input)
 {  
  string s;
  int ret = 0;
  REP(i, input.size())
  {
   for(int j = 1; j < input.size() - i; j++)
   {
    s = input.substr(i, j);
   
    int loc = input.find(s, i+j);
    if(loc != string::npos)
    {
     if(input.find(s, loc+1) != string::npos)
      continue;
     else
      ret = max(ret, (int)s.size());
    }
   }
  }
  return ret;
 }
};

SRM199 DIV2
: string 역순으로 변경을 반복으로 할게 아니라..그냥 reverse(ALL(sFactor))으로 해줘도 된다..

class StringMult
{
public:
 string times(string sFactor, int iFactor)
 {  
  string s;
  if(sFactor.size() == 0 || iFactor == 0) return s;

  int n = sFactor.size();
  if(iFactor > 0)
  {
   REP(i, iFactor)
    s += sFactor;
  }
  else
  {
   string iv;
   REP(i, n)
    iv += sFactor[n -i -1];

   REP(i, abs(iFactor))
    s += iv;
  }

  return s;
 }
};

SRM200 DIV2
: 굳이 oper함수를 만들지 않았어도 돼는 문제였으나, 실전에서는 따로 빼는게 맞는 듯.
e[0]-48 으로 하지않고, e[0]-'0' 으로 짜는게 더 나아보인다.

class NoOrderOfOperations
{
public:
 int oper(char c, int l, int r)
 {
  switch(c)
  {
  case '+':return l+r;
  case '-':return l-r;
  case '*':return l*r;  
  }
  return 0;
 }
 int evaluate(string e)
 {  
  int n = e.size();
  int cur = e[0]-48;

  for(int i=1; i < n; i=i+2)
   cur = oper(e[i], cur, e[i+1]-48);

  return cur;
 }
};

SRM201 DIV2
: 심플한 문제

class Multiples
{
public:

 int number(int min, int max, int f)
 {  
  int cnt = 0;
  for(int i = min; i<= max; i++)
   if(i % f == 0) cnt++;

  return cnt;
 }
};

SRM202 DIV2

class LetterStrings
{
public:

 int sum(vector <string> s)
 {  
  int ret = 0;

  string u;
  REP(i, s.size())
  {
   u = s[i];
   REP(j, u.size())
    if( u[j] != '-') ret++;
  }

  return ret;
 }
};

SRM203 DIV2

class UserName
{
public:

 string toStr(int num)
 {
  stringstream s;
  s << num;
  return s.str();

 }
 string newMember(vector <string> e, string newName)
 {  
  string ret = newName;
  int n = e.size();

  int cnt = 0;
 
  REP(i, n+1)
  {
   if(i != 0) ret = newName + toStr(i);
   cnt = 0;
   REP(j, n)
   {
    if( e[j] == ret)
    {
     cnt = 1;
     break;
    }
   }
   if(cnt == 0) return ret;
  }
 }
};

SRM204 DIV2
: 익숙치 않은 set을 써볼라다가 시간만 소비했다;; set사용법을 익혀둘 필요가 있음.

class Medici
{
public:

 int winner(vector <int> f1, vector <int> f2, vector <int> s)
 {  
  int n = f1.size();
  vector<int> a(n, -1);

  REP(i,n)
   a[i] = min(min(f1[i], f2[i]), s[i]);

  sort(ALL(a));
  reverse(ALL(a));
  if(a[0] == a[1] ) return -1;

  REP(i,n)
   if(a[0] == min(min(f1[i], f2[i]), s[i])) return i;

 }
};

SRM205 DIV2

class Average
{
public:

 int belowAvg(vector <int> m, vector <int> v)
 {  
  int n = m.size();
  int sum =0;
  REP(i,n)
   sum += m[i] + v[i];
  double avg = 1.0 * sum / n;

  int ret = 0;
  REP(i, n)
   if( m[i]+v[i] < avg) ret++;

  return ret;
 }
};

SRM206 DIV2

class Bits
{
public:

 int minBits(int n)
 {    
  REP(i, 100)
  {
   if( n < pow(2.0, i)) return i;
  }
 }
};

SRM207 DIV2

class TransportCounting
{
public:

 int countBuses(int s, vector <int> p, vector <int> v, int t)
 {    
  int my_r = s * t;
  int cnt = 0;
  REP(i, p.size())  
   if( p[i] == 0 || my_r >= p[i] + v[i] * t) cnt++;

  return cnt;
 
 }
};

SRM208 DIV2
: 어디선가 똑같은 문제를 본 듯한데..; SRM145에 나왔던 문제구나;

class ImageDithering
{
public:

 int count(string d, vector <string> s)
 {    
  string sub;
  int cnt = 0;
  REP(i, d.size())
   REP(j, s.size())
    REP(k, s[j].size())
     if(d[i] == s[j][k]) cnt++;

  return cnt;
 }
};

SRM209 DIV2
: 좋은 문제

class MovingAverages
{
public:

 vector <int> calculate(vector <string> t, int n)
 {    
  VI ret;
  int h, m, s;
  int sum = 0;
  char c;
  REP(i, t.size())
  {
   if(t.size() < i+n) break;
   sum = 0;
   for(int j=i; j< i+n; j++)
   {
    stringstream in(t[j]);
    in >> h >> c >> m >> c >> s;
    sum += h * 3600 + m * 60 + s;
   }
   ret.pb(sum/n);
  }
  return ret;
 }
};

SRM210 DIV2
: 새로 string할당할 필요없이 바로 t에서 바꿔줘도 됐다.
if( islower(t[i]) && ((i == 0) || t[i-1] == ' ' ))) 이 경우에만 바꿔주면 된다.

class TitleString
{
public:

 string toFirstUpperCase(string t)
 {    
  string ret;
  int is_first = 1;
  REP(i, t.size())
  {
   if(t[i] == ' ')
   {
    is_first = 1;
    ret += ' ';
   }
   else if(is_first==1 && islower(t[i]))
   {
    is_first = 0;
    ret += toupper(t[i]);
   }
   else
    ret += t[i];
  }
  return ret;
 }
};

반응형