서랍장

[백준-6443] 애너그램 본문

책장/알고리즘

[백준-6443] 애너그램

TERAJOO 2021. 9. 3. 13:29

간단한 탐색 문제이다. 바로 로직을 보면 다음과 같다.

  • 입력받은 문자열을 알파벳 순으로 정렬해준다.
  • map 을 두어 이미 체크한 문자열을 탐색하지 않도록 설정해준다.
  • 그 후 문자열의 idx 에 대해 check 해주며 브루트 포스 탐색을 실시한다.

이러면 그냥 답이 나온다... 간단~

#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include <queue>
#include <tuple>
#include <string.h>
#include <map>

#define INF 100000000

using namespace std;
typedef pair<int, int> pii;
typedef tuple<int, int, int> tiii;

int arr[100001];
int check[100001];
int visit[100001];
int n, t;

void dfs(string x, string cur, map<string, int> &m) {
    if (cur.size() == x.size()) {
        cout << cur << "\n";
        return;
    }
    for (int i = 0; i < x.size(); i++) {
        string cx = cur + x.at(i);
        if (check[i] == 0 && m[cx] == 0) {
            m[cx] = 1;
            check[i] = 1;
            dfs(x, cx, m);;
            check[i] = 0;
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int n;
    string input;
    cin >> n;
    for (int i = 0; i < n; i++) {
        memset(check, 0, sizeof(check));
        cin >> input;
        map<string, int> m;
        sort(input.begin(), input.end());
        dfs(input, "", m);
    }


    return 0;
}
 

6443번: 애너그램

첫째 줄에 단어의 개수 N 이, 둘째 줄부터 N개의 영단어가 들어온다. 영단어는 소문자로 이루어져 있다. 단어의 길이는 20보다 작거나 같고, 애너그램의 수가 100,000개 이하인 단어만 입력으로 주

www.acmicpc.net

 

'책장 > 알고리즘' 카테고리의 다른 글

[백준-1949] 우수마을  (0) 2021.09.20
[백준-17136] 색종이 붙이기  (0) 2021.09.05
[백준-9466] 텀 프로젝트  (0) 2021.09.03
[백준-8111] 0과 1  (0) 2021.09.02