Merged
Conversation
Contributor
Author
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main{
private static BufferedReader br;
private static String T;
private static String S;
private static int[] NABOfT;
private static int[] NABOfS;
private static int neededNB;
private static int neededNA;
private static int na;
private static int nb;
private static int[] perm;
static class QE{
String s;
int na;
int nb;
QE(String s, int na, int nb){
this.s = s;
this.na = na;
this.nb = nb;
}
}
public static void main(String[] args) throws IOException{
br = new BufferedReader(new InputStreamReader(System.in));
// 입력
S = br.readLine();
T = br.readLine();
// nABOfS와 nABOfT를 구함
// S와 T의 각각 maxA, minA, maxB, minB 를 구함
NABOfT = new int[2];
int t1 = 0;
int t2 = 0;
for(char c: S.toCharArray()){
if(c == 'A')
t1++;
else
t2++;
}
NABOfS = new int[]{t1, t2};
t1 = 0;
t2 = 0;
for(char c: T.toCharArray()){
if(c == 'A')
t1++;
else
t2++;
}
NABOfT = new int[]{t1, t2};
na = neededNA = NABOfT[0] - NABOfS[0];
nb = neededNB = NABOfT[1] - NABOfS[1];
boolean isFind = false;
var q = new ArrayDeque<QE>();
q.add(new QE(S, na, nb));
while(!q.isEmpty()){
QE temp = q.poll();
if(temp.s.equals(T)){
isFind = true;
break;
}
var sb = new StringBuilder(temp.s);
var a = temp.na;
var b = temp.nb;
if(a != 0){
sb.append('A');
q.add(new QE(sb.toString(), a - 1, b));
}
sb = new StringBuilder(temp.s);
if(b != 0){
sb.append('B');
sb.reverse();
q.add(new QE(sb.toString(), a, b-1));
}
}
System.out.println(isFind? 1:0);
br.close();
}
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🧷 문제 링크
https://www.acmicpc.net/problem/12919
🧭 풀이 시간
110 분
👀 체감 난이도
✏️ 문제 설명
🔍 풀이 방법
⏳ 회고
다음부터 S->T로 만드는 문제일때, 역으로도 생각해보는 습관을 기르자