기본 콘텐츠로 건너뛰기

1874 : 스택 수열 (Stack) [C++]

#include <iostream>

using namespace std;

class Stack
{
private:
 int Array[100000];
 int Top;
public:
 Stack(int n);
 void Push(int n);
 void Pop();
 int GetTop();
 int GetSize();
};

Stack::Stack(int n)
{
 Top = 0;
}

void Stack::Push(int n)
{
 Array[Top++] = n;
}

void Stack::Pop()
{ 
 Top--;
}

int Stack::GetTop()
{
 return Array[Top - 1];
}

int Stack::GetSize()
{
 return Top;
}

int main()
{
 int N;
 cin >> N;

 Stack Data(0);
 bool Digit[100000];
 
 for (int i = 0; i < 100000; i++)
  Digit[i] = true;

 int j_init = 1;

 for (int i = 0; i < N; i++)
 {
  int Temp;
  cin >> Temp;
  
  for (int j = j_init; j <= Temp; j++)
  {
   if (Digit[j - 1])
   {
    Data.Push(j);
    Digit[j - 1] = false;
    cout << "+" << endl;
   }
  }

  for (int j = Data.GetTop(); j >= Temp; j--)
  {
   Data.Pop();
   cout << "-" << endl;
  }

  j_init = Data.GetTop();
 }
}

댓글

이 블로그의 인기 게시물

11478 : 서로 다른 부분 문자열의 개수 (미제) [C++]

# include < iostream > # include < vector > # include < string > using namespace std ; int main ( ) { string sInput ; getline ( cin , sInput , '\n' ) ; int Time = 1 ; int Count = 0 ; vector < string > Storage ; for ( int i = 0 ; i < sInput . size ( ) ; i + + ) { for ( int j = 0 ; ( j + Time - 1 ) < sInput . size ( ) ; j + + ) Storage . push_back ( sInput . substr ( j , Time ) ) ; Time + + ; } bool * Visited = new bool [ Storage . size ( ) * sizeof ( bool ) ] ; for ( int i = 0 ; i < Storage . size ( ) ; i + + ) { Visited [ i ] = true ; for ( int j = 0 ; j < Storage . size ( ) ; j + + ) { if ( i ! = j & & Storage [ i ] = = Storage [ j ] ) { Visited [ i ] = false ; Visited [ j ] = true ; break ; } } } for ( int i = 0 ; i < Storage . size ( ) ; i + + ) if ( Visited [ i ] ) Count ...

6359 : 만취한 상범 (Dynamic Programming) [C++]

# include < iostream > # include < vector > using namespace std ; int Num_of_Divisor ( int n ) { int Count = 0 ; for ( int i = 1 ; i < = n ; i + + ) if ( n % i = = 0 ) Count + + ; return Count ; } int main ( ) { int Testcase ; cin > > Testcase ; while ( Testcase - - ) { int Rooms ; cin > > Rooms ; vector < int > Prisons ; for ( int i = 0 ; i < Rooms ; i + + ) Prisons . push_back ( 0 ) ; for ( int i = 1 ; i < Prisons . size ( ) + 1 ; i + + ) { if ( ( Num_of_Divisor ( i ) % 2 ) = = 0 ) Prisons [ i - 1 ] = 0 ; else Prisons [ i - 1 ] = 1 ; } int Fleer = 0 ; for ( int i = 0 ; i < Prisons . size ( ) ; i + + ) if ( Prisons ...

1520 : 내리막길(미제) (Dynamic Programming) [C,C++]

답은 알맞게 나오는 데 시간이 자꾸 초과된다. 3시간 째 붙잡고 있지만 플러드 필 문제로 밖에 생각이 안되는 데 플러드 필을 사용하면 시간이 오바가 나고.. 경로의 수를 각 배열 인덱스에 저장하면서 나아가는 것 같기도 한데, 사실 상 이것도 플러드 필과 다를 바 없어서 내일 시험 끝나고 다른 방법을 고안해봐야할 것 같다.