第一行一个整数n(1 ≤ n ≤ 50,000)。 第二行一个字符串t(长度 ≤ 1,000,000)
"yes"表示存在满足条件的k,否则输出"no"
8 01112
yes
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
bool checkExistSubstring(string a, string b){
if(a.find(b) != string::npos)
return true;
else
return false;
}
string tranmistDecToKth(int n, int k) {
string sb;
while (n != 0) {
if (n % k > 9)
sb += ((n % k - 10 + 'A'));
else
sb += ((n % k) + '0');
n /= k;
}
reverse(sb.begin(), sb.end());
return sb;
}
string getDifference(int n, string t) {
for (int k = 2; k <= 16; k++) {
string str;
for (int i = 1; i <= n; i++) {
string s = tranmistDecToKth(i, k);
str += s;
}
if(checkExistSubstring(str, t))
return "yes";
}
return "no";
}
int main() {
int n;
string str;
while (cin >> n && cin >> str)
cout << getDifference(n, str) << endl;
return 0;
}
求指教。老是通过率60%(已修正)
import java.util.*;
public class Main {
public static String getDifference(int n, String t) {
for (int k = 2; k <= 16; k++) {
StringBuffer res = new StringBuffer();
for (int i = 1; i <= n; i++) {
String str = tranmistDecToKth(i, k);
res.append(str);
}
if(checkExistSubstring(res.toString(),t))
return "yes";
}
return "no";
}
public static boolean checkExistSubstring(String a,String b){
if(a.indexOf(b) != -1)
return true;
else
return false;
}
public static String tranmistDecToKth(int n, int k) {
StringBuffer sb = new StringBuffer();
while (n != 0) {
if (n % k > 9)
sb.append((char) (n % k + 55));
else
sb.append(n % k);
n /= k;
}
//System.out.println(sb.reverse().toString());
return sb.reverse().toString();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
// int k = sc.nextInt();
String s = sc.next();
String t = sc.nextLine();
System.out.println(getDifference(n, t));
}
}