끄적끄적
SWE 원재의 메모리 복구하기 본문
그리디 문제로 앞에서부터 같은 위치에서 정답과 현재 문자열의 문자가 다르면 현재 위치부터 끝까지 숫자를 바꾼다.
public class SwExpertMemoryRecovery {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int test = Integer.parseInt(br.readLine());
for (int t = 0; t < test; t++) {
String s = br.readLine();
int[] ans = new int[s.length()];
for (int i = 0; i < s.length(); i++) {
ans[i] = s.charAt(i) - '0';
}
int[] str = new int[s.length()];
int cnt = 0;
for (int i = 0; i < str.length; i++) {
if (ans[i] != str[i]) {
for (int j = i; j < str.length; j++) {
str[j] = ans[i];
}
cnt++;
}
}
System.out.println("#" + (t + 1) + " " + cnt);
cnt = 0;
}
}
}
'알고리즘' 카테고리의 다른 글
백준 17478 재귀함수가 뭔가요? (0) | 2021.08.06 |
---|---|
백준 1244 스위치 켜고끄기 (0) | 2021.08.06 |
SWE 달뱅이 숫자 (0) | 2021.08.06 |
SWE Ladder1 (0) | 2021.08.06 |
SWE Flatten (0) | 2021.08.06 |
Comments