Outdated/Algorithm Solution

[BOJ] 1544번 사이클 단어

해달 2020. 6. 29. 18:00

복기

-


코드

  • C++ 17


#include <iostream>

#include <string>

#include <set>

#include <algorithm>

#include <string.h>

 

using namespace std;

 

int N;

set<string> WordSet;

 

bool Check(string& word)

{

    // WordSet에 있는 모든 단어와 확인한다.

    for (const string& src : WordSet)

    {

        if (word.size() != src.size())

            continue;

 

        for (int i = 0; i < word.size(); ++i)

        {

            if (src == word)

                return true;

 

            // 단어 크기만큼 단어를 돌려본다.

            rotate(word.begin(), word.begin() + 1, word.end());

        }

    }

 

    return false;

}

 

int main()

{

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    cout.tie(nullptr);

 

    cin >> N;

    for (int i = 0; i < N; ++i)

    {

        string word;

        cin >> word;

 

        if (false == Check(word))

            WordSet.insert(word);

    }

    

    cout << WordSet.size();

}


  • C# 6.0


using System;

using System.Collections.Generic;

using System.Text;

 

namespace Csharp

{

    class Program

    {

        static HashSet<string> Set = new HashSet<string>();

        static StringBuilder SB = new StringBuilder();

 

        static void Main(string[] args)

        {

            int N = Convert.ToInt32(Console.ReadLine());

            for (int i = 0; i < N; ++i)

            {

                string word = Console.ReadLine();

                if (false == Check(word))

                    Set.Add(word);

            }

 

            Console.WriteLine(Set.Count);

        }

 

        static bool Check(string word)

        {

            SB.Clear();

            SB.Append(word);

 

            foreach (string src in Set)

            {

                for (int i = 0; i < word.Length; ++i)

                {

                    if (src == SB.ToString())

                        return true;

 

                    SB.Append(SB[0]).Remove(0, 1);

                }

            }

            return false;

        }

    }

}


  • Python 3


N = int(input())

WordSet = set()

 

def check(word):

    global WordSet

 

    wordLen = len(word)

    for src in WordSet:

        if len(src) != wordLen:

            continue

 

        for i in range(wordLen):

            if src == word:

                return True

            

            word = f"{word[1:]}{word[0]}"

    

    return False

 

for i in range(N):

    word = input()

    if not check(word):

        WordSet.add(word)

print(len(WordSet))



'Outdated > Algorithm Solution' 카테고리의 다른 글

[BOJ] 5585번 거스름돈  (0) 2020.10.07
[BOJ] 9935번 문자열 폭발  (0) 2020.07.06
[BOJ] 1074번 Z  (0) 2020.06.29
[BOJ] 1016번 제곱 ㄴㄴ수  (0) 2020.06.22
[BOJ] 1476번 날짜 계산  (0) 2020.06.22