728x90
반응형
1. 그룹 단어 체커(백준 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 IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(); int num = Integer.parseInt(br.readLine()); int cnt = num; for(int i=0; i<num; i++) { String word = br.readLine(); boolean[] tf = new boolean[26]; int first = word.charAt(0)-97; tf[first] = true; for(int j=1; j<word.length(); j++) { char now = word.charAt(j); if(now == word.charAt(j-1)) continue; if(tf[now-97]) { cnt--; break; } tf[now-97] = true; } } System.out.println(sb.append(cnt)); } }
2. 단어 공부(백준 1157번)
https://www.acmicpc.net/problem/1157
▷ 풀이 코드
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String a = sc.next(); a = a.toUpperCase(); int arr[] = new int[40]; int max = 0; int b = 0; for(int i=0; i<a.length(); i++) { arr[a.charAt(i)-65]++; } for(int i=0; i<a.length(); i++) { if(max < arr[a.charAt(i)-65]) { max = arr[a.charAt(i)-65]; b = a.charAt(i)-65; } } for(int i=0; i<arr.length; i++) { if(max == arr[i] && i != b) { b = -1; break; } } if(b == -1) { System.out.println("?"); } else { System.out.println((char)(b + 65)); } } }
3. 열 개씩 끊어 출력하기(백준 11721번)
https://www.acmicpc.net/problem/11721
▷ 풀이 코드
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String a = sc.next(); a = a.toUpperCase(); int arr[] = new int[40]; int max = 0; int b = 0; for(int i=0; i<a.length(); i++) { arr[a.charAt(i)-65]++; } for(int i=0; i<a.length(); i++) { if(max < arr[a.charAt(i)-65]) { max = arr[a.charAt(i)-65]; b = a.charAt(i)-65; } } for(int i=0; i<arr.length; i++) { if(max == arr[i] && i != b) { b = -1; break; } } if(b == -1) { System.out.println("?"); } else { System.out.println((char)(b + 65)); } } }
2. 단어 공부(백준 1157번)
https://www.acmicpc.net/problem/1157
▷ 풀이 코드
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String a = sc.next(); a = a.toUpperCase(); int arr[] = new int[40]; int max = 0; int b = 0; for(int i=0; i<a.length(); i++) { arr[a.charAt(i)-65]++; } for(int i=0; i<a.length(); i++) { if(max < arr[a.charAt(i)-65]) { max = arr[a.charAt(i)-65]; b = a.charAt(i)-65; } } for(int i=0; i<arr.length; i++) { if(max == arr[i] && i != b) { b = -1; break; } } if(b == -1) { System.out.println("?"); } else { System.out.println((char)(b + 65)); } } }
4. 잃어버린 괄호(백준 1541번)
https://www.acmicpc.net/problem/1541
▷ 풀이 코드
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(); String input[] = br.readLine().split("-"); int sum = 0; for(int i=0; i<input.length; i++) { String plus[] = input[i].split("\\+"); for(int j=0; j<plus.length; j++) { if(i==0) { sum += Integer.parseInt(plus[j]); } else { sum -= Integer.parseInt(plus[j]); } } } System.out.println(sum); } }
이번에는 알고리즘 스터디에서 공부했던 문자열 통해 백준 문제 풀이를 해보았습니다!!
문자열에서는 알파벳의 개수만큼 배열을 지정하면서 글자들을 비교하니 아주 편리하네요!
더 다양한 문제들을 풀어보면서 문제 풀이 방법들에 대해서 익혀볼게요~
많은 분들의 피드백은 언제나 환영합니다! 많은 댓글 부탁드려요~~
728x90
반응형
'문제풀기 > 백준 문제풀이' 카테고리의 다른 글
[알고리즘] 브루트포스 문제 풀이(백준 7568번, 1018번) (2) | 2023.08.24 |
---|---|
[알고리즘] 이분 탐색 문제 풀이(백준 2805번, 1654번, 2512번) (0) | 2023.08.24 |
[알고리즘] 해시맵 문제 풀이(백준 10815번, 14425번) (0) | 2023.08.18 |
[알고리즘] 데크 문제 풀이(백준 10866번) (2) | 2023.08.10 |
[알고리즘] 큐 문제 풀이(백준 10845번, 1158번, 1966번) (2) | 2023.08.10 |