회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
2
채점 현황
랭킹
게시판
그룹
더 보기
재채점 기록
블로그
강의
실험실
도움말
BOJ Stack
BOJ Book
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
왜 틀렸을까요 java입니다.
1316번 - 그룹 단어 체커
wjdqo1113
2년 전
0
게시물 반례들 몇 개 넣어봐도 자꾸 틀리다고 나옵니다.
import java.io.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int size = Integer.parseInt(br.readLine()); String[] str = new String[size]; if(size>100) return; String str1 = ""; int sum = 0; for(int i = 0; i<size; i++) { str[i] = br.readLine(); if(str[i].length()>100) return; } for(int i = 0; i<str.length; i++) { int count=1; if(str[i].contains(" ")||str[i]==null||str[i].isEmpty()) { count=0; } else { for(int j = 0; j<str[i].length(); j++) { if((j+1)<str[i].length())//문자열의 길이보다 (j+1)이 작을 때 { if(str[i].charAt(j)!=str[i].charAt(j+1))//j번째 문자와 j가 다른 문자일 때 { if(str1.contains(Character.toString(str[i].charAt(j)))) { count=0; } else { str1+=Character.toString(str[i].charAt(j)); } if((j+1)==(str[i].length()-1)) { if(str1.contains(Character.toString(str[i].charAt(j+1)))) { count=0; } else { str1+=Character.toString(str[i].charAt(j+1)); } } } } } } str1=""; sum+=count; } System.out.println(sum); } }
dldyddlwl
2년 전
1
아래에 있습니다.
// ... 코드 중략 for(int i = 0; i<str.length; i++) { // ... 코드 중략 else { for(int j = 0; j<str[i].length(); j++) { if((j+1)<str[i].length()) { if(str[i].charAt(j)!=str[i].charAt(j+1))// 1번 if문 { // ... 코드 중략 if((j+1)==(str[i].length()-1)) // 문제는 이 부분이었습니다. { // 이 부분이 위에 있는 1번 if문 밖으로 나와야 합니다! if(str1.contains(Character.toString(str[i].charAt(j+1)))) { count=0; } else { str1+=Character.toString(str[i].charAt(j+1)); } } if(str[i].charAt(j)!=str[i].charAt(j+1))// 1번 if문 {} // 그리고 위의 코드 // 즉, 다시 말하면 같지 않을 때도 조사해야하지만, 같을 때도 조사해야 한다는 것입니다. // 반례는 abaaaaaaaaaaaaaaaaaa 입니다. // 이 경우, a와 b가 달라서 str1에 a가 저장되고, b와 a가 달라서 b가 저장됩니다. // 그런데, 결국 뒤에 있는 a들이 다 같으므로, 위의 1번 if문을 거치지 않게 됩니다. // 그러면 그룹문자가 아닌데도 불구하고 count가 되는 것이지요! // 따라서, 문자가 같은 경우에도 마지막을 조사할 필요가 있다는 것입니다!!! // 예시 for(int j = 0; j<str[i].length(); j++) { if((j+1)<str[i].length())//문자열의 길이보다 (j+1)이 작을 때 { if(str[i].charAt(j)!=str[i].charAt(j+1))//j번째 문자와 j가 다른 문자일 때 { if(str1.contains(Character.toString(str[i].charAt(j)))) { count=0; } else { str1+=Character.toString(str[i].charAt(j)); } } if((j+1)==(str[i].length()-1)) { if(str1.contains(Character.toString(str[i].charAt(j+1)))) { count=0; } else { str1+=Character.toString(str[i].charAt(j+1)); } } } }
댓글을 작성하려면
로그인
해야 합니다.
wjdqo1113 2년 전
게시물 반례들 몇 개 넣어봐도 자꾸 틀리다고 나옵니다.