File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ public class Main {
4+ static int N ;
5+ static boolean found = false ;
6+
7+ public static void main (String [] args ) throws IOException {
8+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
9+ N = Integer . parseInt(br. readLine(). trim());
10+ dfs(" " , 0 );
11+ }
12+
13+ static void dfs (String s , int depth ) {
14+ if (found) return ;
15+ if (depth == N ) {
16+ System . out. println(s);
17+ found = true ;
18+ return ;
19+ }
20+ for (int i = 1 ; i <= 3 ; i++ ) {
21+ String ns = s + i;
22+ if (goodString(ns)) {
23+ dfs(ns, depth + 1 );
24+ }
25+ if (found) return ;
26+ }
27+ }
28+
29+ static boolean goodString (String s ) {
30+ int len = s. length();
31+ for (int l = 1 ; l <= len / 2 ; l++ ) {
32+ String a = s. substring(len - 2 * l, len - l);
33+ String b = s. substring(len - l, len);
34+ if (a. equals(b)) {
35+ return false ;
36+ }
37+ }
38+ return true ;
39+ }
40+ }
41+ ```
You can’t perform that action at this time.
0 commit comments