1246
问题描述
试题编号: 202006-4
试题名称: 1246
时间限制: 1.0s
内存限制: 512.0MB
Java语言32分
测试9–25,n的值太大,运行时间不过关。(水平实在低,拿一分算一分)
Java代码如下
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String [] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str_n = br.readLine();
int n = Integer.valueOf(str_n);
StringBuilder s_orig = new StringBuilder("1");
String s_sub = br.readLine();
for(int i = 1;i <= n;i++){
int s_length = s_orig.length();
int index = 0;
for(int j = 0;j < s_length;j++,index++){
switch(s_orig.charAt(index)){
case '1':
s_orig.replace(index, (index+1), "2");
break;
case '2':
s_orig.replace(index, (index+1), "4");
break;
case '4':
s_orig.replace(index, (index+1), "16");
index++; // 一位变两位,索引再加一
break;
case '6':
s_orig.replace(index, (index+1), "64");
index++;
break;
}
}
}
String res = s_orig.toString();
int res_length = res.length();
int sub_length = s_sub.length();
long count = 0;
for(int i = 0;i <= res_length-sub_length;i++){
if(res.charAt(i) == s_sub.charAt(0) && res.substring(i, i+sub_length).equals(s_sub)){
count++;
count %= 998244353;
}
}
System.out.println(count);
}
}
若有好方法,欢迎评论!