본문 바로가기
코딩테스트

백준1316

by 멈추지않아 2022. 10. 14.
백준1316:https://www.acmicpc.net/problem/1316
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
  public static void main(String[] args) throws NumberFormatException, IOException {
    // scan 처럼 글자 받아오기
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int cou = Integer.parseInt(br.readLine());
    StringBuilder sb = new StringBuilder();
    int index = 0;
    int before = -1;
    char str;
    int sum = 0;
    int none = 0;
    
    // scan 처럼 글자 받아오기
    for (int c = 0; c < cou; c++) {
      String fir = br.readLine();
      for (int i = 0; i < fir.length(); i++) {
        //한글자씩 담기
        str = fir.charAt(i);
        //받아온 한글자의 인덱스
        index = fir.indexOf(str);
        //같은그자중에 현재 전에 있는 글잘의 인덱스
        before = -1;
        while (index != -1) {
         //before-1이라느건 젤 첫번째 이므로 비교할수가 없기때문에 검사 안하고 
          //두수의 차이가 1이 아니라는건은 두칸 이상 떨어 져있는거기 때문에 그룹 함수가 아니다
          if (before != -1 && (index - before) != 1) {
           //그룹함수 아닌 글자의 갯수
            sum++;
            //while문 탈출하게 만들기
            index = -1;
          } else {
            //위에 조건이 아니면 아직 단정 짓기 힘들기때문에 before에 현재 index저장
            before = index;
            //index다음중 같은 글자의 인덱스를 저장
            index = fir.indexOf(str, index + 1);
          }
        }
        
        
      }
      if (sum > 0) {
       //sum0보다 큰것이 있는것은 그룹함수가 아니기 때문에 none++
        none++;
        //sum은 그룹함수가 아닌걸 확인하는 용이기때문에 매글자마다 초기화 해줘야되기때문에 초기화 실행
        sum = 0;
      }
    }
    //그룹함수 아닌거에 입력받은 전체 단어수를 빼주면 완성
    sb.append(cou-none);
    System.out.println(sb);
  }
}

'코딩테스트' 카테고리의 다른 글

백준5622  (0) 2022.10.13
백준2908  (0) 2022.10.12
백준 4344번  (0) 2022.10.11
백준 8958번  (0) 2022.10.10