HackerEarth 中的问题陈述:
一个大的二进制数由大小为 N 的字符串 A 表示,由 0 和 1 组成。您必须对此字符串执行循环移位。循环移位操作定义如下:
如果字符串 A 是 [A0,A1,A2,...,AN−1],则在执行一次循环移位后,字符串变为 [A1,A2,...,AN−1,A0]。您执行了无限次移位,并且每次都记录了字符串表示的二进制数的值。执行(可能为 0)操作后形成的最大二进制数是 B。您的任务是确定可以执行的循环移位数,以使字符串 A 表示的值第 K 次等于 B。
然而,为了清除所有示例案例,我在提交时遇到了运行时错误。有人能告诉我哪里出错了吗?代码:
import java.util.*;
class TestClass {
public static void main(String args[] ) throws Exception {
Scanner s = new Scanner(System.in);
long N = s.nextInt();
for(int o=0;o<N;o++) //to loop through test cases
{
long n1 = s.nextInt();
long k = s.nextInt();
String S = s.next();
int max = 0;
int d,n=0,j,p=-1; // p for periodicity in the given inout which influence on max('B') //repetation
char str;
for(int i=0;i<n1;i++) //To find max and also periodicity
{
StringBuffer b = new StringBuffer(S);
d = Integer.parseInt(S,2);
if(max<d)
{
max = d;
n=i;
}
else if(max == d)
{
p=i-n;
break;
}
str = S.charAt(0); //to left shift with constant complexity
b.deleteCharAt(0);
S = b.toString()+Character.toString(str);
}
if(p==-1)
{
System.out.println((n+(k-1)*n1)%(10^9+7));
}
else
System.out.println((n+(k-1)*p)%(10^9+7));
}
}
}