모름

이 문제는 조건에 맞는 문자열을 찾는 문제입니다.

 · 코드전문 미리보기

...더보기
class Program {
    static void Main(string[] args)
    {
        string input = Console.ReadLine();
        int N = int.Parse(input);

        string[] words = WordInputer(N);
        int countGroupWords = 0;

        for (int i = 0; i < words.Length; i++)
        {
            if(IsGroupWord(words[i]))
            {
                countGroupWords++;
            }
        }

        Console.WriteLine(countGroupWords);
    }

    static string[] WordInputer(int N)
    {
        string[] words = new string[N];
        for (int i = 0; i < N; i++)
        {
            words[i] = Console.ReadLine();
        }
        return words;
    }

    static bool IsGroupWord(string word)
    {
        for (int i = 0; i < word.Length; i++)
        {
            for (int j = i; j < word.Length; j++)
            {
                if (j - i > 1)
                {
                    if (word[i] == word[j])
                    {
                        return false;
                    }
                }

                if(word[i] == word[j])
                {
                    i += j - i;
                }
            }
        }
        return true;
    }
}

 

 

 

문제요약

 

그룹 단어를 찾는 문제입니다. 그룹 단어란 단어 내 모든 문자에 대해서 각 문자가 연속해서 나타는 경우만 존재한다면 그룹 단어라 칭할 수 있습니다. 예를 들어 aaabb는 a와 b가 연속해서 나타나기 때문에 그룹단어입니다. 반대로 aaabba 는 a와 b중 a가 연속해서 쓰이지 않았기 때문에 그룹단어가 아닙니다.

 

 

 

입력출력

 

입력

 · 검사할 단어의 개수 N이 주어집니다. 둘째 줄 부터 N개의 단어가 들어옵니다.

출력

 · 입력된 단어들 중 그룹단어의 개수를 출력합니다.

 

예제 입출력

...더보기

예제 입력(1)
 · 3
 · happy
 · new
 · year
예제 출력(1)
 · 3

 

예제 입력(2)
 · 4
 · aba
 · abab
 · abcabc
 · a
예제 출력(2)
 · 1

 

 

 


 

풀이1 - 핵심 기능의 구현

 

먼저 한 단어에 대해서 그룹단어인지 아닌지 체크하는 함수를 만들어야합니다.

 

1) 단어를 철자별로 검사할 isCroupWord(word: string) 함수를 만들어줍니다.
2) 한 번 검사한 알파벳은 checked 라는 bool값 배열을 통해 체크됐다고 표시합니다.
3) 각 철자별로 다른 알파벳이 나타날때까지 연속해서 체크해줍니다.
4) checked된 알파벳이 다시 한 번 등장하면 그룹단어가 아니므로 false를 리턴합니다.
5)
모든 알파벳이 정상적으로 검사 됐으면 true를 리턴합니다.

 

isCroupWord(word: string) //그룹단어인지 체크해주는 함수

static bool isGroupWord(string word)
{
    for (int i = 0; i < word.Length; i++)
    {
        for (int j = i; j < word.Length; j++)
        {
            if (j - i > 1)
            {
                if (word[i] == word[j])
                {
                    return false;
                }
            }

            if(word[i] == word[j])
            {
                i += j - i;
            }
        }
    }
    return true;
}

그룹단어인지 체크해주는 함수를 만들었습니다. 앞선 풀이에선 bool값을 통해 알파벳이 체크됐는지 확인해준다고 했는데, 막상 함수를 작성하니 bool값을 사용할 필요가 없었습니다. 단지 i번째 알파벳 2칸 뒤의 알파벳부터 같은 알파벳이 있는지 계속 검사해주고, 같은게 나오면 false를 리턴합니다.

 

그 외에는 word[i]와 word[j]가 반복문을 통해 같은지 계속 비교해주고, 같은 철자가 나온 수만큼 i번 반복을 건너 띄어줌으로서 반복 횟수를 줄여주고 중복 검사를 없엤습니다. 그리고 모든 검사가 완료되면 이 단어는 그룹단어이므로 true를 리턴해줍니다.

 

 

 


 

풀이2 - 핵심 기능을 입력받은 단어만큼 반복

그룹단어인지 아닌지 체크하는 함수를 이용하면 출력하는 건 어렵지 않습니다.

 

static string[] WordInputer(int N)
{
    string[] words = new string[N];
    for (int i = 0; i < N; i++)
    {
        words[i] = Console.ReadLine();
    }
    return words;
}

먼저 입력받은 N번의 횟수만큼 단어를 입력받는 함수를 만들어줬습니다. 

 

static void Main(string[] args)
{
    string input = Console.ReadLine();
    int N = int.Parse(input);

    string[] words = WordInputer(N);
    int countGroupWords = 0;

    for (int i = 0; i < words.Length; i++)
    {
        if(isGroupWord(words[i]))
        {
            countGroupWords++;
        }
    }

    Console.WriteLine(countGroupWords);
}

앞서 작성한 함수를 가지고 코드를 작성해줍니다. N번의 횟수를 입력받고, N개의 단어를 입력받습니다. N개의 단어를 IsGroupWord(word: string) 함수를 이용해 그룹단어인지 체크합니다. true값이 나온 만큼 카운트를 해주고 카운트 값을 출력시켜주면 완성입니다.

 

 

 


 

출력 - 예제 입출력을 통한 결과 확인

 

정상 출력됩니다. 이상입니다.

 

코드전문보기

...더보기
class Program {
    static void Main(string[] args)
    {
        string input = Console.ReadLine();
        int N = int.Parse(input);

        string[] words = WordInputer(N);
        int countGroupWords = 0;

        for (int i = 0; i < words.Length; i++)
        {
            if(IsGroupWord(words[i]))
            {
                countGroupWords++;
            }
        }

        Console.WriteLine(countGroupWords);
    }

    static string[] WordInputer(int N)
    {
        string[] words = new string[N];
        for (int i = 0; i < N; i++)
        {
            words[i] = Console.ReadLine();
        }
        return words;
    }

    static bool IsGroupWord(string word)
    {
        for (int i = 0; i < word.Length; i++)
        {
            for (int j = i; j < word.Length; j++)
            {
                if (j - i > 1)
                {
                    if (word[i] == word[j])
                    {
                        return false;
                    }
                }

                if(word[i] == word[j])
                {
                    i += j - i;
                }
            }
        }
        return true;
    }
}