【Java】蓝桥快读快写模板

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Scanner;
import java.util.StringTokenizer;

//import static java.lang.System.out;

public class Main {

	//Scanner 内部使用了 InputStreamReader
	static Scanner sc = new Scanner(new BufferedInputStream(System.in, 1024));
//	static BufferedReader br = new BufferedReader(new InputStreamReader(new BufferedInputStream(System.in, 2048)), 1024);
//  static PrintWriter pw = new PrintWriter(System.out, true); //自动刷新
    static BufferedReader br = new BufferedReader(new InputStreamReader(new BufferedInputStream(System.in, 8192 << 3)), 8192 << 2);
    //PrintWriter: 内置 new BufferedWriter(new OutputStreamWriter())
    //PrintWriter: 更多的数据输出方法,但内部缓冲大小是默认的 BufferedWriter,当输出流是控制台时, 流对象将是 PrintStream
    static PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out, 8192 << 3));
    //可控的缓冲大小,可控刷新流,append 添加,write 写入,但写出 int | long | double | boolean 没有整型浮点型类型,只有String和char (可以自己转换成 String 写入)
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new BufferedOutputStream(System.out, 8192 << 3)), 8192 << 2);
    
    static class IN1 { //从输入流读取的每个字节被视为'\u0000'到'\u00FF'范围内的字符。 字符值用于查找字符的五个可能属性: 空格 , 字母 , 数字 , 字符串引号和注释字符
    	//charBufferSize 8192 一次读取字符个数, 8192 * 2 = 16384 b字节 / 1024 = 16 kb
    	static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(new BufferedInputStream(System.in, 8192 << 8)), 8192 << 7));
    	static int nextInt() throws IOException {in.nextToken(); return (int) in.nval;}
    	static String next() throws IOException {in.nextToken(); return in.sval;}
    	static long nextLong() throws IOException {in.nextToken(); return (long) in.nval;}
        static double nextDouble() throws IOException {in.nextToken(); return in.nval;}
        static byte nextByte() throws IOException {in.nextToken(); return (byte) in.nval;}
        static char nextChar() throws IOException {in.nextToken(); return in.sval.charAt(0);} //注意只有 length == 1 时
    }
    
    static class IN2 { //是字符就读取
    	static BufferedReader br = new BufferedReader(new InputStreamReader(new BufferedInputStream(System.in, 8192 << 7)), 8192 << 6);
    	static StringTokenizer st;
    	static String next() {
    		while (st == null || !st.hasMoreTokens()) {try {st = new StringTokenizer(br.readLine());} catch (IOException e) {e.printStackTrace();}}
    		return st.nextToken();
    	}
    	static int nextInt() {return Integer.parseInt(next());}
    	static String nextLine() {String str = null;try {str = br.readLine();} catch (IOException e) {e.printStackTrace();}return str;}
    	static long nextLong() {return Long.parseLong(next());}
    	static byte nextByte() {return Byte.parseByte(next());}
    	static char nextChar() {return next().charAt(0);} //注意只有 length == 1 时
    	static double nextDouble() {return Double.parseDouble(next());}
    }
    
    static class Time {
    	{
	    	long beginTime = System.currentTimeMillis();
	    	// run coding ....
	    	long endTime = System.currentTimeMillis();
	    	System.out.println(endTime - beginTime + "ms");
    	}
    }

    public static void main(String[] args) throws Exception {
    	
    }
   

}

由于福尔摩斯问题的具体要求不明确,我将假设题目指的是以下问题: 给定一个字符串,判断其中是否存在一个子串,使得该子串中每个字符出现的次数都相同。 例如,对于字符串 "abbcc",存在子串 "bb",其中 'b' 出现了 2 次,'c' 出现了 2 次,满足每个字符出现的次数相同。 以下是使用 Java 实现的代码: import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); int n = s.length(); Map<Character, Integer> freq = new HashMap<>(); for (int i = 0; i < n; i++) { char c = s.charAt(i); freq.put(c, freq.getOrDefault(c, 0) + 1); } boolean found = false; for (int len = 2; len <= n; len++) { for (int i = 0; i <= n - len; i++) { Map<Character, Integer> subFreq = new HashMap<>(); for (int j = i; j < i + len; j++) { char c = s.charAt(j); subFreq.put(c, subFreq.getOrDefault(c, 0) + 1); } if (subFreq.equals(freq)) { System.out.println(s.substring(i, i + len)); found = true; break; } } if (found) { break; } } if (!found) { System.out.println("NONE"); } } } 代码说明: 1. 首先读入字符串并计算每个字符出现的次数,使用 Map 存储。 2. 从长度为 2 的子串开始枚举,依次找出每个长度的子串并计算其中每个字符出现的次数,同样使用 Map 存储。 3. 判断当前子串中每个字符出现的次数是否与原字符串中每个字符出现的次数相同,如果相同则输出该子串并结束程序。 4. 如果没有找到符合条件的子串,则输出 "NONE"。 该代码的时间复杂度为 O(n^3),可以通过此题。实际上,还有更优秀的算法可以将时间复杂度优化到 O(n^2) 或 O(n) 级别,但由于题目数据范围小,此处不赘述。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虚妄狼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值