AtCoder ABC 029 A&B&C AtCoder - 029
I'm just doing Typical DP Contest and Go lang and I haven't touched it at all I wonder if I will focus on the C and D problems that have not been solved anymore.
private void solveA() {
String s = next();
out.println(s + "s");
}
private void solveB() {
int res = 0;
for (int i = 0; i < 12; i++) {
String s = next();
int cnt = s.indexOf("r");
if (cnt >= 0) {
res++;
}
}
out.println(res);
}
――The alternative solution was a waste of memory and I felt it was slow, so I rewrote it, but it didn't change. .. ..
--The point is the order of s +" a "
. .. .. .. Not so much.
private void solveC() {
int numN = nextInt();
saikiC(numN, "");
}
private void saikiC(int n, String s) {
if (n == 0) {
out.println(s);
return;
}
for (int i = 0; i < 3; i++) {
switch (i) {
case 0:
saikiC(n - 1, s + "a");
break;
case 1:
saikiC(n - 1, s + "b");
break;
case 2:
saikiC(n - 1, s + "c");
break;
default:
break;
}
}
}
private void solveC2() {
int numN = nextInt();
Set<String> wk = new TreeSet<String>();
printC2(numN, 1, 1, "", wk);
printC2(numN, 1, 2, "", wk);
printC2(numN, 1, 3, "", wk);
for (String string : wk) {
out.println(string);
}
}
private void printC2(int n, int current, int index, String s, Set<String> wk) {
if (current > n) {
wk.add(s);
return;
}
String tmp = s;
switch (index) {
case 1:
tmp = tmp + "a";
break;
case 2:
tmp = tmp + "b";
break;
case 3:
tmp = tmp + "c";
break;
default:
break;
}
printC2(n, current + 1, 1, tmp, wk);
printC2(n, current + 1, 2, tmp, wk);
printC2(n, current + 1, 3, tmp, wk);
}
Recommended Posts