首页 > 试题广场 >

子串

[编程题]子串
  • 热度指数:912 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
给出一个正整数n,我们把1..n在k进制下的表示连起来记为s(n,k),例如s(16,16)=123456789ABCDEF10, s(5,2)=11011100101。现在对于给定的n和字符串t,我们想知道是否存在一个k(2 ≤ k ≤ 16),使得t是s(n,k)的子串。

输入描述:
第一行一个整数n(1 ≤ n ≤ 50,000)。
第二行一个字符串t(长度 ≤ 1,000,000)


输出描述:
"yes"表示存在满足条件的k,否则输出"no"
示例1

输入

8
01112

输出

yes
import java.util.Scanner;

public class Main {
private static StringBuffer sb=new StringBuffer();
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int n=in.nextInt();
in.nextLine();
String t=in.nextLine();
System.out.println(getDifference(n,t));
}

private static String getDifference(int n, String t) {
StringBuffer sBuffer=new StringBuffer();
for(int i=2;i<=16;i++){
for(int j=1;j<=n;j++){
String string=getString(i,j,sb);
sBuffer.append(string);
}
if(sBuffer.toString().contains(t)){
return "yes";
}else{
sBuffer.delete(0, sBuffer.length());
}
}
return "no";
}

public static  String getString(int i, int j,StringBuffer sb) {
if(sb.length()>0){
sb.delete(0, sb.length());
}
//StringBuffer sb=new StringBuffer();
   while (j != 0) {
       if (j % i > 9)
       sb.append((char)(j % i  + 55));
       else
       sb.append(j%i);
       j /= i;
   }
   return sb.reverse().toString();
}
}

发表于 2017-07-13 18:53:48 回复(0)
#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;
}

发表于 2017-06-26 21:52:04 回复(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));
	}
}

编辑于 2017-09-07 21:42:09 回复(6)

热门推荐

通过挑战的用户