본문 바로가기

문제풀기/백준 문제풀이

[알고리즘] 문자열 문제 풀이(백준 1316번, 1157번, 11721번, 1541번)

728x90
반응형

1. 그룹 단어 체커(백준 1316번)

 

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

 

1316번: 그룹 단어 체커

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

www.acmicpc.net

 

▷ 풀이 코드

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

 

1157번: 단어 공부

알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.

www.acmicpc.net

 

▷ 풀이 코드

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

 

11721번: 열 개씩 끊어 출력하기

첫째 줄에 단어가 주어진다. 단어는 알파벳 소문자와 대문자로만 이루어져 있으며, 길이는 100을 넘지 않는다. 길이가 0인 단어는 주어지지 않는다.

www.acmicpc.net

 

▷ 풀이 코드

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

 

1157번: 단어 공부

알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.

www.acmicpc.net

 

▷ 풀이 코드

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

 

1541번: 잃어버린 괄호

첫째 줄에 식이 주어진다. 식은 ‘0’~‘9’, ‘+’, 그리고 ‘-’만으로 이루어져 있고, 가장 처음과 마지막 문자는 숫자이다. 그리고 연속해서 두 개 이상의 연산자가 나타나지 않고, 5자리보다

www.acmicpc.net

 

▷ 풀이 코드

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
반응형