처음에는 map<이름, 번호> 맵 하나로 풀었다가 시간초과가 났다. 
입력 개수가 최대 10만이기 때문에 하나의 map으로는 시간초과가 난다. 
그래서 map을 2개 만들어놔서 풀었다.  
 
하나는 이름을 입력하면 번호를 값으로 갖는 smap<이름, 번호> 이다. 
다른건 숫자를 입력하면 이름을 값으로 갖는 nmap<번호, 이름> 이다. 
이렇게 하면 반복문을 안돌아도 값을 바로 찾을 수 있다. 
 
#include <bits/stdc++.h>
using namespace std;

int n, m; // 포켓몬개수, 문제개수
string name, input;
map<string, int> smap; // 문자 입력 -> 숫자출력
map<int, string> nmap; // 숫자 입력 -> 문자출력

int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> m;
  for(int i = 1; i <= n; i++){
    cin >> name;
    smap.insert({name, i}); // {이름, 인덱스}
    nmap.insert({i, name}); // {인덱스, 이름}
  }

  for(int i = 0; i< m; i++){
    cin >> input;
    if(isdigit(input[0])){ // 숫자라면 0이 아닌 수가 나온다
      cout << nmap[atoi(input.c_str())]<< '\n';
    }else{ // 문자
      cout << smap[input] << '\n';
    }
  }

  return 0;
}
 
728x90

'알고리즘 > 백준' 카테고리의 다른 글

최소 힙 백준 1927번 c++  (0) 2022.02.19
ATM 백준 11399번 c++  (0) 2022.02.19
최대 힙 백준 11279번 c++  (0) 2022.02.19
집합 백준 11723번 c++  (0) 2022.02.19
바이러스 백준 2602번 c++  (0) 2022.02.19
 
항상 최대값이 뭔지 알고 있어야 하니까, priority_queue를 이용했다. 
우선순위 큐의 top()은 항상 최대 값을 알고 있다. 
pop() 하면 항상 최대값이 제거된다. 
 
#include <bits/stdc++.h>
using namespace std;

int n;
long long input;
priority_queue<long long> pq;

int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n;

  while(n--){
    cin >> input;
    if(pq.empty() && input == 0){
      cout << 0 << '\n';
    }else if(input == 0){ // 출력 및 제거
      cout << pq.top() << '\n';
      pq.pop();
    }else{
      pq.push(input);
    }
  }

  return 0;
}
 
728x90

문제 링크 

 
있다, 없다를 검사하는 연산이 많다.  
 
입력받는 숫자 x의 범위가 20 이하의 자연수다. 
 
따라서 20개를 담을 수 있는 bool 타입의 배열을 선언하여 숫자의 존재유무를 표시하면 된다.  
 
또는 비트 연산자를 활용하면 된다. 
 
 
#include <bits/stdc++.h>
using namespace std;

int m, num, x;
string command;

int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> m;
  while(m--){
    cin >> command;
    if(command == "add"){
      cin >> num;
      x |= (1 << num); // OR 연산 num 번째 자리 수를 1로 채운다
    }
    else if(command=="remove"){
      cin >> num;
      x &= ~(1 << num); // 반전시킨 num을 AND 연산한다.
    }
    else if(command =="check"){
      cin >> num;
      if( x & (1 << num))
        cout << 1 << '\n';
      else
        cout << 0 << '\n';
    }else if(command =="toggle"){
      cin >> num;
      x ^= (1 << num); // num 번째 자리가 1이면 1^1 = 0,  0이라면 0^1 = 1
    }
    else if(command == "all"){
      x = (1 << 21) -1;
    }else if(command =="empty"){
      x = 0;
    }
  }

  return 0;
}
 
728x90
 
문제는 1번 컴퓨터에서 무조건 시작하도록 되어 있다. 
 
1번 컴퓨터와 인접한 컴퓨터들을 알아야 하니까 인접리스트를 이용해서 확인한다. 
 
1번 컴퓨터에서 가장 깊이 들어간 노드 개수를 세면 되니까 dfs(1)로 시작해서 개수를 센다. 
 
 
#include <bits/stdc++.h>
using namespace std;

int n, m, i, j;
vector<int> v[102]; // 인접리스트
bool visited[102]; // 방문체크
int cnt; // 1과 인접 개수

void dfs(int x){
  visited[x] = true; //방문
  for(int i = 0; i < v[x].size(); i++){
    int nextnode = v[x][i];
    if(!visited[nextnode]){
      dfs(nextnode);
      cnt++;
    }
  }
}
int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> m;

  while(m--){
    cin >> i >> j;
    v[i].push_back(j);
    v[j].push_back(i);
  }
  dfs(1); // 1번은 이미 걸렸고, 1번 때문에 바이러스 걸리는 컴퓨터의 개수
  cout << cnt;
  return 0;
}
 
728x90

'알고리즘 > 백준' 카테고리의 다른 글

최대 힙 백준 11279번 c++  (0) 2022.02.19
집합 백준 11723번 c++  (0) 2022.02.19
비밀번호 찾기 백준 17219번 c++  (0) 2022.02.17
저작권 백준 2914번 c++  (0) 2022.02.17
민균이의 비밀번호 백준 9933번 c++  (0) 2022.02.17

문제 링크 

 

주소와 비밀번호 쌍을 입력받는다. 

주소를 통해 비밀번호를 찾는 것이고, 주소는 중복되지 않는다.  map의 키는 중복되지 않으니까 map으로 구현했다. 

 

"맞았습니다" 코드 링크  

#include <bits/stdc++.h>
using namespace std;

int n, m;
string address, password;
map<string, string> smap;

int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> m;
  while(n--){
    cin >> address >> password;
    smap.insert({address, password});
  }
  while(m--){
    cin >> address;
    cout << smap[address] << '\n';
  }

  return 0;
}
728x90

'알고리즘 > 백준' 카테고리의 다른 글

집합 백준 11723번 c++  (0) 2022.02.19
바이러스 백준 2602번 c++  (0) 2022.02.19
저작권 백준 2914번 c++  (0) 2022.02.17
민균이의 비밀번호 백준 9933번 c++  (0) 2022.02.17
막대기 백준 1094번 c++  (0) 2022.02.17
 
평균을 구할 때 반드시 ‘올림'을 하라는 것이 힌트였다. 
32.1 , 32.8이 나와도 출력을 33으로 해야 한다. 
따라서 (평균값 - 1 ) 에서 곡 개수를 곱한 다음, 다시 +1 해준다. 
 
#include <bits/stdc++.h>
using namespace std;

double total_count, avg_value; // 수록곡 개수, 평균값

int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> total_count >> avg_value;
  cout << (avg_value - 1) * total_count + 1;

  return 0;
}
 
 
 
728x90

'알고리즘 > 백준' 카테고리의 다른 글

바이러스 백준 2602번 c++  (0) 2022.02.19
비밀번호 찾기 백준 17219번 c++  (0) 2022.02.17
민균이의 비밀번호 백준 9933번 c++  (0) 2022.02.17
막대기 백준 1094번 c++  (0) 2022.02.17
수들의 합 백준 1789번 c++  (0) 2022.02.16
 
팰린드롬 문자인지, 문자열을 뒤집은 문자를 포함하는지를 확인해야 한다. 
“문자열을 뒤집은 문자를 포함하는지” 확인하면, 자기 자신이 팰린드롬일 때 이 조건을 이미 만족한다. 
 
#include <bits/stdc++.h>
using namespace std;

int n;
string st, answer;

int mmmmmain(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n;

  vector<string> v(n);
  for(int i = 0; i < n; i++){
    cin >> v[i];
  }
  for(int i = 0; i < n; i++){
    st = v[i];
    reverse(st.begin(), st.end());
    if( st == *find(v.begin(), v.end(), st)) {
      answer = st; break;
    }
  }
  int asize = answer.size();
  cout << asize << ' ' << answer[asize/2];
  return 0;
}
728x90

'알고리즘 > 백준' 카테고리의 다른 글

비밀번호 찾기 백준 17219번 c++  (0) 2022.02.17
저작권 백준 2914번 c++  (0) 2022.02.17
막대기 백준 1094번 c++  (0) 2022.02.17
수들의 합 백준 1789번 c++  (0) 2022.02.16
좌표 압축 백준 18870번 c++  (0) 2022.02.16

+ Recent posts