본문 바로가기
알고리즘/백준 BOJ

[ BOJ/C# ] 1316 그룹 단어 체커

by 왹져박사 2023. 10. 30.
728x90

https://www.acmicpc.net/problem/1316

 

1316번: 그룹 단어 체커

그룹 단어란 단어에 존재하는 모든 문자에 대해서, 각 문자가 연속해서 나타나는 경우만을 말한다. 예를 들면, ccazzzzbb는 c, a, z, b가 모두 연속해서 나타나고, kin도 k, i, n이 연속해서 나타나기 때

www.acmicpc.net

아스키코드를 활용하였다. 

using System;
using System.IO;

namespace B1316
{
    class Program
    {
        static void Main()
        {
            StreamReader sr = new StreamReader(Console.OpenStandardInput());
            StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());

            int n = int.Parse(sr.ReadLine());
            bool[] alphabet = new bool[26];
            int count = 0;
            for(int i=0; i < n; i++)
            {
                string str = sr.ReadLine();
                bool isGroup = true;    //그룹단어인가?
                char preChar = str[0];  //이전 문자
                alphabet[str[0] - 97] = true;   
                for (int j = 1; j < str.Length; j++)
                {
                    if (alphabet[str[j] - 97] == true && preChar != str[j])
                    {
                        //알파벳이 이미 나왔고 연속한 문자가 아닐 경우
                        isGroup = false;
                        break;
                    }
                    preChar = str[j];
                    alphabet[str[j] - 97] = true;
                }
                if (isGroup) count++;
                Array.Clear(alphabet);
            }
            sw.Write(count);
            sr.Close();
            sw.Flush();
            sw.Close();
        }
    }
}

728x90

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

[ BOJ/C# ] 12789 도키도키 간식드리미  (0) 2023.11.01
[ BOJ/C# ] 2193 이친수  (0) 2023.10.31
[ BOJ/C# ] 4949 균형잡힌 세상  (0) 2023.10.29
[ BOJ/C# ] 11050 이항 계수 1  (1) 2023.10.28
[ BOJ/C# ] 2108 통계학  (0) 2023.10.27