字串统计

问题描述

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

输入格式

  第一行一个数字L。

  第二行是字符串S。

  L大于0,且不超过S的长度。

输出格式

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

 

输入样例1:

  4

  bbaabbaaaaa

 

输出样例1:

  bbaa

 

输入样例2:

  2

  bbaabbaaaaa

 

输出样例2:

  aa

数据规模和约定

  n<=60

  S中所有字符都是小写英文字母。

 

提示

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

 

参考代码:

#include<stdio.h>
#include<string.h>
#define N 100
int main(){
	char a[N]={0},b[N],c[N];
	int i,j,k,len,l,count,max=0,lenB=0,lenC;
	scanf("%d",&l);
	getchar();
	gets(a);
	len=strlen(a);
	for(;l<len;l++){
		for(i=0;i<len-l;i++){
			for(j=i,k=0;j<l+i;j++,k++)
				c[k]=a[j];
			c[k]=0;
			count=1;
			for(j=i+1;j<len-l;j++){
				for(k=j;k<j+l;k++){
					if(a[k]!=c[k-j])
						break;
				}
				if(k==j+l)
					count++;
			}
			lenC=strlen(c);
			if(count>1&&(count>max||(count==max&&lenB<lenC))){
				for(j=0;j<=lenC;j++)
					b[j]=c[j];
				lenB=lenC;
				max=count;
			}
		}
	}
	puts(b);
	return 0;
} 


测试结果:



在 Java 中,统计字符串中最长子串可以根据不同的条件和需求采用不同的方法,以下为几种常见的情况及实现: ### 至少有 K 个重复字符的最长子串 可使用递归分治的方法,统计每个字母出现的次数,排除频次小于 k 的字符,再递归处理剩余子串。 ```java class Solution { public int longestSubstring(String s, int k) { int len = s.length(); if (len == 0 || k > len) { return 0; } if (k < 2) { return len; } return count(s.toCharArray(), k, 0, len - 1); } private static int count(char[] chars, int k, int left, int right) { if (right - left + 1 < k) return 0; int[] times = new int[26]; // 26个字母 for (int i = left; i <= right; ++i) { times[chars[i] - 'a']++; // 统计每个字母出现的次数,字符出现频次小于k,则不可能出现在结果子串中 } // 分别排除,然后挪动两个指针 while (right - left + 1 >= k && times[chars[left] - 'a'] < k) { ++left; } while (right - left + 1 >= k && times[chars[right] - 'a'] < k) { --right; } if (right - left + 1 < k) { // 排除到剩余的字符串小于k,则直接return return 0; } // 得到临时子串,再递归处理 for (int i = left; i <= right; ++i) { // 如果第i个不符合要求,切分成左右两段分别递归求得 if (times[chars[i] - 'a'] < k) { return Math.max(count(chars, k, left, i - 1), count(chars, k, i + 1, right)); } } return right - left + 1; } } ``` 使用示例: ```java public class Main { public static void main(String[] args) { Solution solution = new Solution(); String s = "ababbc"; int k = 2; System.out.println(solution.longestSubstring(s, k)); } } ``` ### 无重复字符的最长子串 可使用滑动窗口的思想,定义两个指针 `start` 和 `end`,利用 `HashSet` 存放符合条件的子串,遍历字符串并动态调整窗口。 ```java import java.util.HashSet; public class LongestSubstringWithoutRepeatingCharacters { public int lengthOfLongestSubstring(String s) { int start = 0; int end = 0; int maxLength = 0; HashSet<Character> set = new HashSet<>(); while (end < s.length()) { if (!set.contains(s.charAt(end))) { set.add(s.charAt(end)); end++; maxLength = Math.max(maxLength, set.size()); } else { set.remove(s.charAt(start)); start++; } } return maxLength; } public static void main(String[] args) { LongestSubstringWithoutRepeatingCharacters solution = new LongestSubstringWithoutRepeatingCharacters(); String s = "abcabcbb"; System.out.println(solution.lengthOfLongestSubstring(s)); } } ``` ### 长度大于等于 L 且出现次数最多的子串 可以枚举所有可能的子串,统计其出现次数,找出符合条件的子串。 ```java import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class SubstringStatistics { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int L = scanner.nextInt(); scanner.nextLine(); String S = scanner.nextLine(); String result = findMaxSubstring(S, L); System.out.println(result); } public static String findMaxSubstring(String S, int L) { Map<String, Integer> substringCount = new HashMap<>(); int n = S.length(); for (int i = 0; i < n; i++) { for (int j = i + L; j <= n; j++) { String sub = S.substring(i, j); substringCount.put(sub, substringCount.getOrDefault(sub, 0) + 1); } } String maxSubstring = ""; int maxCount = 0; for (Map.Entry<String, Integer> entry : substringCount.entrySet()) { String sub = entry.getKey(); int count = entry.getValue(); if (count > maxCount || (count == maxCount && sub.length() > maxSubstring.length()) || (count == maxCount && sub.length() == maxSubstring.length() && S.indexOf(sub) < S.indexOf(maxSubstring))) { maxCount = count; maxSubstring = sub; } } return maxSubstring; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值