Java语言实现 ALGO-87    字串统计(算法训练)



  算法训练 字串统计  

时间限制:1.0s   内存限制:512.0MB

 

问题描述

  给定一个长度为n的字符串S,还有一个数字L,统计长度大于等于L出现次数最多子串不同的出现可以相交),如果有多个,输出最长的,如果仍然有多个,输出第一次出现最早的

输入格式

  第一行一个数字L。
  第二行是字符串S。
  L大于0,且不超过S的长度。

输出格式

  一行,题目要求的字符串。

  输入样例1:
  4
  bbaabbaaaaa

  输出样例1:
  bbaa

  输入样例2:
  2
  bbaabbaaaaa

  输出样例2:
  aa

数据规模和约定

  n<=60
  S中所有字符都是小写英文字母。

解题思路:枚举所有可能的子串,统计出现次数,找出符合条件的那个

参考代码:

import java.util.Scanner;

public class Main {
	static String maxstr;
	static int maxcount;
	String maxstrl;
	int maxcountl;
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		int l=in.nextInt();
		String str=in.next();
		
		Main m=new Main();
		m.MaxSubStr(l,str);
		maxstr=m.maxstrl;
		maxcount=m.maxcountl;
		for(int i=l+1;i<=str.length();i++) {
			m.MaxSubStr(i,str);
			if(m.maxcountl>=maxcount) {//如果有多个,输出最长的
				maxstr=m.maxstrl;
				maxcount=m.maxcountl;
			}
		}
		System.out.println(maxstr);
	}
	
	//统计长度等于L的出现次数最多的子串(不同的出现可以相交)
	private  void MaxSubStr(int l, String str) {
		int n=str.length()-l+1;
//		System.out.println(n);
		String[] arr=new String[n];
		int[] count=new int[n];
		for(int i=0;i<n;i++) {
			arr[i]="";
		}
		for(int i=0;i<n;i++) {
			arr[i]=str.substring(i, i+l);
//			System.out.println(arr[i]);
		}
		
		for(int j=0;j<n;j++) {
			if(arr[0].equals(arr[j])) {
				count[0]++;
			}
		}
		int maxcountl=count[0];
		String maxstrl = arr[0];
		for(int i=1;i<n;i++) {
			for(int j=0;j<n;j++) {
				if(arr[i].equals(arr[j])) {
					count[i]++;
				}
			}
			if(count[i]>maxcountl) {//如果仍然有多个,输出第一次出现最早的
				maxcountl=count[i];
				maxstrl=arr[i];
			}
		}
		
		this.maxstrl=maxstrl;
		this.maxcountl=maxcountl;
	}

}

我的解题方法没有用到java中的集合等强大工具,只是为了快速解题,常人思维,清晰易懂,解题思路适用于所有语言。如果想顺便提高java编程能力的话,可以参考以下链接:

https://sihailoveyan.iteye.com/blog/2370217

https://blog.csdn.net/SZStudy/article/details/71327104

看不懂的,下面有我添加了注释的代码可以参考:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
 
public class Main{
	static StringBuilder s;
	public static void main(String[] args) throws IOException  {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		s = new StringBuilder(br.readLine());
		
		HashSet<String> set = new HashSet<>();
		String s2="";
		int max = 0;//max表示最多出现次数
		int start = 0;//start,end表示出现次数最多子串的位置
		int end = 0;
		
		while(n<=s.length()){//统计长度大于等于L的出现次数最多的子串
			for (int i = 0; i <= s.length()-n; i++) {
				s2 = s.substring(i, i+n);
				if(set.add(s2)){
					int num=getNum(s,s2);
					//如果有多个,输出最长的;如果仍然有多个,输出第一次出现最早的。
					if(num > max || (max==num &&(end - start)<n)){
						start = i;
						end = i+n;
						max = num;
					}
				}
			}
			n++;
		}
		System.out.println(s.substring(start, end));
	}

	private static int getNum(StringBuilder s2, String s22) {//统计子串s22出现在串s2中的次数
		int num = 0;
		int index = s2.indexOf(s22, 0);//返回指定子字符串第一次出现的字符串中的索引,从指定的索引开始。
		while(index!=-1){
			num++;
			index = s2.indexOf(s22, index+1);
			
		}
		return num;
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值