/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
{
// your code goes here
f623("111");
f623("1110");
f623("101100");
}
/**
* 1のビットがN個ある二進表記文字列が与えられたとき、
* 次に大きい1のビットがN個ある二進表記文字列を求める。
* @param s 1のビットがN個ある二進表記文字列
* @return 次に大きい1のビットがN個ある二進表記文字列
*/
List<String> list = new ArrayList<String>(s.length());
int n = 0;
for (int i = 0; i < s.length(); i++) {
int j = (s.length() - 1) - i;
if (s.regionMatches(j, "1", 0, 1)) {
list.add(i, "1"); // リテラル(コンスタントプール)を入れるので
n++;
} else if (s.regionMatches(j, "0", 0, 1)) {
list.add(i, "0");
} else {
}
}
if (n == 0) {
}
int c = 0;
while (c != n) {
int d = 1;
c = 0;
for (int i = 0; i < list.size(); i++) {
if (d == 0) {
if ("1" == list.get(i)) { // equals()使わなくてOK
c++;
if (c > 3) {
break;
}
}
} else {
if ("1" == list.get(i)) {
list.set(i, "0");
} else {
list.set(i, "1");
d = 0;
c++;
}
}
}
if (d == 1) {
list.add("1");
}
}
StringBuilder dest = new StringBuilder(list.size());
dest.append(p);
}
String result
= dest.
reverse().
toString(); System.
out.
println(s
+ " -> " + result
); return result;
}
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgovKiBOYW1lIG9mIHRoZSBjbGFzcyBoYXMgdG8gYmUgIk1haW4iIG9ubHkgaWYgdGhlIGNsYXNzIGlzIHB1YmxpYy4gKi8KY2xhc3MgSWRlb25lCnsKCXB1YmxpYyBzdGF0aWMgdm9pZCBtYWluIChTdHJpbmdbXSBhcmdzKSB0aHJvd3MgamF2YS5sYW5nLkV4Y2VwdGlvbgoJewoJCS8vIHlvdXIgY29kZSBnb2VzIGhlcmUK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