Codeforces 문제 푸는 길 - 711A Bus to Udayland

3929 단어 Codeforces
A. Bus to Udayland
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
ZS the Coder and Chris the Baboon are travelling to Udayland! To get there, they have to get on the special IOI bus. The IOI bus has nrows of seats. There are 4 seats in each row, and the seats are separated into pairs by a walkway. When ZS and Chris came, some places in the bus was already occupied.
ZS and Chris are good friends. They insist to get a pair of neighbouring empty seats. Two seats are considered neighbouring if they are in the same row and in the same pair. Given the configuration of the bus, can you help ZS and Chris determine where they should sit?
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the number of rows of seats in the bus.
Then, n lines follow. Each line contains exactly 5 characters, the first two of them denote the first pair of seats in the row, the third character denotes the walkway (it always equals '|') and the last two of them denote the second pair of seats in the row.
Each character, except the walkway, equals to 'O' or to 'X'. 'O' denotes an empty seat, 'X' denotes an occupied seat. See the sample cases for more details.
Output
If it is possible for Chris and ZS to sit at neighbouring empty seats, print "YES"(without quotes) in the first line. In the next n lines print the bus configuration, where the characters in the pair of seats for Chris and ZS is changed with characters '+'. Thus the configuration should differ from the input one by exactly two charaters (they should be equal to 'O' in the input and to '+' in the output).
If there is no pair of seats for Chris and ZS, print "NO"(without quotes) in a single line.
If there are multiple solutions, you may print any of them.
Examples
input
6
OO|OX
XO|XX
OX|OO
XX|OX
OO|OO
OO|XX

output
YES
++|OX
XO|XX
OX|OO
XX|OX
OO|OO
OO|XX

input
4
XO|OX
XO|XX
OX|OX
XX|OX

output
NO

input
5
XX|XX
XX|XX
XO|OX
XO|OO
OX|XO

output
YES
XX|XX
XX|XX
XO|OX
XO|++
OX|XO

Note
Note that the following is an incorrect configuration for the first sample case because the seats must be in the same pair.
O+|+X
XO|XX
OX|OO
XX|OX
OO|OO
OO|XX
제목의 뜻: 이미 알고 있는 객차의 좌석 상황, 예를 들어'OO|XX'는'|'를 통로로 하고 통로를 중점으로 하며 좌우는 한 조의pair이고 O는empty를 대표하며 X는 이미 자리를 차지했다.두 사람이 앉을 수 있도록 페어의 빈 좌석이 있는지 알려 달라고 요구했다.있으면 YES를 출력하고 최신 좌석 분포도를 출력합니다. 이 두 승객의 좌석 위치는++로 구분됩니다.없으면 NO를 직접 출력합니다.
문제풀이 사고방식: 이 문제는 상당히 간단하다. 줄마다 좌석을 스캔하면'OO'가 존재하는지 확인하고 표시가 있으면 NO를 출력한다.
여기서 나는 자바 문자열contains(CharSequence arg0)로 어느 줄에'OO'가 있는지 판단하고, 찾으면 ReplaceFirst(String regex,String replacement)로 처리하고 출력을 출력하면 된다.
다음은 문제풀이 코드(java 구현)
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		scanner.nextLine();
		String[] strings = new String[n];
		
		for(int i = 0;i < n;i++){
			strings[i] = scanner.nextLine();
		}
		
		int row = 0;
		boolean flag = false;
		for(int i = 0;i < n;i++){
			if(strings[i].contains("OO")){
				row = i;
				flag = true;
				break;
			}
		}
		if(flag){
			String str = strings[row].replaceFirst("OO", "++");
			System.out.println("YES");
			for(int i = 0;i < n;i++){
				if(i == row){
					System.out.println(str);
				}else{
					System.out.println(strings[i]);
				}			
			}
		}else{
			System.out.println("NO");
		}		
	}
}

좋은 웹페이지 즐겨찾기