输入一个长度为
、由数字和小写字母混合构成的字符串
。保证至少存在一个数字子串。
记最长的数字子串长度为
,有
个长度为
的数字子串。在一行上先首尾相连的输出
个长度为
的数字子串(不使用空格分割),随后输出一个逗号,再输出
。
abcd12345ed125ss123058789
123058789,9
11a22b33c
112233,2
在这个样例中,数字子串
长度均为
,都是最长的数字子串。
本题数据已规范为单组询问(2025/01/15)。
import java.util.HashSet;
import java.util.Scanner;
import java.util.TreeSet;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.nextLine();
str = str.replaceAll("[a-z]", "*");
str = str.replaceAll("\\*{1,}", "*");
String[] str0 = str.split("\\*");
int maxlen = 0;
for (int i = 0; i < str0.length; i++) {
maxlen = Math.max(maxlen, str0[i].length());
}
for (int i = 0; i < str0.length; i++) {
if(str0[i].length() == maxlen){
System.out.print(str0[i]);
}
}
System.out.println(","+maxlen);
}
} java实现:先用str来接收控制台过来的字符串,并转为char[] chars(这样运行速率比用String快),用max保存过程中最大的连续数字串长度。
当遇到数字时,count计数加一,用dp[i]记录该索引值的count值,期间总是更新count的最大值max;当遇到字符时,count归零,等再次遇到数字字符时,count重新从0开始计数。
依次遍历结束后,遍历dp数组,当dp[i] == max时,说明找到目标子串,输出即可。
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String str = in.nextLine();
int[] dp = new int[str.length()];
int max = 0, nowcount = 0;
char[] chars = str.toCharArray();
for (int i = 0; i < chars.length; i++){
char c = chars[i];
if (c >= '0' && c <= '9'){
nowcount++;
dp[i] = nowcount;
max = Math.max(nowcount, max);
}else{
nowcount = 0;
dp[i] = nowcount;
}
}
for (int i = 0; i < dp.length; i++){
if (dp[i] == max){
String res = str.substring(i - max + 1, i + 1);
System.out.print(res);
}
}
System.out.println("," + max);
}
}
} import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNext()) { // 注意 while 处理多个 case
String str = in.nextLine();
int[][] res = new int[str.length()][str.length()];
int maxLen = process(str.toCharArray(), 0, str.length() - 1, res);
String s = "";
for (int i = 0, j = maxLen-1 ; j < str.length() ; i++,j++) {
if(res[i][j] == maxLen){
s += str.substring(i , i+maxLen);
}
}
System.out.println(s + ',' + maxLen);
}
}
public static int process(char[] str, int left, int right, int[][] res) {
if (res[left][right] != 0) {
return res[left][right];
}
if (left == right) {
res[left][right] = isNumber(str[left]) ? 1 : 0;
} else {
int p1 = process(str, left + 1, right, res);
int p2 = process(str, left, right - 1, res);
if (p1 == right - left) {
p1 += isNumber(str[left]) ? 1 : 0;
}
if (p2 == right - left) {
p2 += isNumber(str[right]) ? 1 : 0;
}
res[left][right] = Math.max(p1, p2);
}
return res[left][right];
}
public static boolean isNumber(char c) {
return c >= '0' && c <= '9';
}
} import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.next();
List<String> list = new ArrayList<>(); // list存当前最长的数字子串
int max = 0, i = 0, j;
// 双指针i,j 找数字子串,遍历一遍即可
while (i < s.length()) {
while (i < s.length() && !Character.isDigit(s.charAt(i))) i++; // 数字子串 头
j = i;
while (j < s.length() && Character.isDigit(s.charAt(j))) j++; // 数字子串 尾
if (j - i >= max) {
if (j - i > max)
list.clear(); // 更大数字子串,需先清空list,再加入list
max = j - i;
list.add(s.substring(i, j));
}
i = j;
}
list.forEach(System.out::print);
System.out.print("," + max);
}
} import java.util.*;
import java.util.regex.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String temp = in.nextLine();
Pattern pattern = Pattern.compile("([0-9]{1,})");
Matcher matcher = pattern.matcher(temp);
int length = 0;
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
if (matcher.group(1) == null) return;
String current = matcher.group(1);
if (current.length() > length) {
length = current.length();
sb.delete(0, sb.length());
sb.append(current);
} else if (current.length() == length) {
length = current.length();
sb.append(current);
}
}
System.out.println(sb.toString() + "," + length);
}
}
}
//一维动态规划解法
import java.util.*;
public class Main{
public static void main(String[] args) throws Exception{
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
String line = sc.nextLine();
char [] chars = line.toCharArray();
//dp[i]表示以i结尾时最长数字子串的长度
int[] dp = new int[chars.length];
//第一个字符是否为数字
if(Character.isDigit(chars[0])) {
dp[0] = 1;
}else {
dp[0]=0;
}
int max =0;
//填表
for(int i = 1; i < chars.length; i++){
if(Character.isDigit(chars[i])){
dp[i]=1;
if(dp[i-1]>0)
dp[i] += dp[i-1];
}else{
dp[i]=0;
}
if(max<dp[i])max=dp[i];
}
//输出
for (int i = 0; i < chars.length; i++) {
if(dp[i]==max) {
System.out.print(line.substring(i-max+1,i+1));
}
}
System.out.print(",");
System.out.print(max);
System.out.println();
}
}
}
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str = sc.nextLine();
Map<Integer, String> map = new HashMap<>();
int left = 0, right = left + 1;
while (right <= str.length()) {
String substr = str.substring(left, right);
boolean isAllDigit = false;
for (char c : substr.toCharArray()) {
if (!Character.isDigit(c)) {
left += 1;
right = left + 1;
break;
}
isAllDigit = true;
}
if (isAllDigit) {
int len = right - left;
map.put(len, map.getOrDefault(len, "") + substr);
right++;
}
}
int maxKey = 0;
for (Map.Entry<Integer, String> entry : map.entrySet()) {
Integer key = entry.getKey();
maxKey = Math.max(maxKey, key);
}
System.out.println(map.get(maxKey) + "," + maxKey);
}
}
} import java.util.Scanner;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Iterator;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str = sc.next();
System.out.println(getMaxString(str).getKey() + "," + getMaxString(
str).getValue());
}
}
public static Map.Entry<String, Integer> getMaxString(String str1) {
StringBuilder sb = new StringBuilder("");
LinkedHashMap <String, Integer> hm = new LinkedHashMap();
for (int i = 0; i < str1.length();) {
char c = str1.charAt(i);
if (c >= '0' && c <= '9') {
sb.append(c);
i++;
if (i < str1.length()) {
char c2 = str1.charAt(i);
if (c2 >= '0' && c2 <= '9') {
continue;
} else {
hm.put(sb.toString(), sb.length());
sb.replace(0, sb.length(), "");
}
} else if (i == str1.length()) {
char c2 = str1.charAt(i - 1);
if (c2 >= '0' && c2 <= '9') {
hm.put(sb.toString(), sb.length());
} else {
hm.put(String.valueOf(str1.charAt(i)), 1);
}
}
} else {
i++;
}
}
int max = 0;
//求得长度最大值
for (Map.Entry<String, Integer> entry : hm.entrySet()) {
if (entry.getValue() > max) {
max = entry.getValue();
}
}
sb.replace(0, sb.length(), "");
//迭代器遍历删除小于最大值的元素
Iterator<Map.Entry<String, Integer>> it = hm.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Integer> entry = it.next();
if (entry.getValue() < max) {
it.remove();
} else {
//相同长度的字符串拼接在一起
sb.append(entry.getKey());
}
}
hm.clear();
hm.put(sb.toString(), max);
for (Map.Entry<String, Integer> entry : hm.entrySet()) {
return entry;
}
return null;
}
} import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String s = in.nextLine();
String[] sp = s.split("[^\\d]+");
int maxLength = 0;
String result = "";
for (int i = 0; i < sp.length; i++) {
if (maxLength < sp[i].length()) {
result = sp[i];
maxLength = sp[i].length();
} else if (maxLength == sp[i].length()) {
result = result + sp[i];
}
}
System.out.println(result + "," + maxLength);
}
}
} import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNextLine()){
String s = in.nextLine();
List<String> list = new ArrayList();
int max = 0;
int[] dp = new int[s.length()+1];
for(int i = 1; i <= s.length(); i++){
if(Character.isDigit(s.charAt(i-1))){
dp[i] = dp[i-1] +1;
if(dp[i] == max){
list.add(s.substring(i-max, i));
}else if(dp[i] > max){
max = dp[i];
list.clear();
list.add(s.substring(i-max, i));
}
}
}
list.forEach(k->System.out.print(k));
System.out.println(","+ max);
}
}
} import java.lang.Math;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
String inputStr = in.nextLine();
String[] strSplit = inputStr.replaceAll("[^0-9]", ",").split(",");
// 计算最长长度
int maxLeng = 0;
for (String a : strSplit) {
maxLeng = Math.max(maxLeng, a.length());
}
for (String a : strSplit) {
if (a.length() == maxLeng) {
System.out.print(a);
}
}
System.out.print("," + maxLeng + "\n");
}
}
} import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) { // 注意 while 处理多个 case
char[] str=in.nextLine().toCharArray();
int[] DP=new int[str.length+1];
List<Integer> list=new ArrayList<>();
int max=0;
for(int i=1;i<=str.length;i++){
if(str[i-1]>='0'&&str[i-1]<='9'){
DP[i]=DP[i-1]+1;
if(DP[i]>max){
if(!list.isEmpty()){
list.clear();
list.add(i-1);
max=DP[i];
}else{
list.add(i-1);
max=DP[i];
}
}else if(DP[i]==max){
list.add(i-1);
}
}
}
for(int i=0;i<list.size();i++){
int index=list.get(i)-max+1;
for(int j=0;j<max;j++){
System.out.print(str[index+j]);
}
}
System.out.println(","+max);
}
}
} import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = null;
while ((str = br.readLine()) != null) {
int k = 0, count = 0, startIndex = 0, currentIndex = 0;
StringBuilder sb = new StringBuilder();
int length = str.length();
for (int i = 0; i < length; i++) {
char ch = str.charAt(i);
if ('0' <= ch && ch <= '9') {
count++;
if (k < count) {
k = count;
currentIndex = startIndex;
// 清空 StringBuilder
sb.setLength(0);
} else if (k == count) {
sb.append(str.substring(currentIndex, k + currentIndex));
currentIndex = startIndex;
}
} else {
count = 0;
//当前分支是非数字,需要+1
startIndex = i + 1;
}
}
System.out.println(sb.append(str.substring(currentIndex,
k + currentIndex) + "," + k));
}
}
} import java.util.*;
import java.util.regex.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
String input = in.nextLine();
Pattern p = Pattern.compile("[0-9]+");
Matcher m = p.matcher(input);
int length = 0;
String numStr = "";
while (m.find()) {
String nums = m.group();
if (nums.length() > length) {
length = nums.length();
numStr = nums;
} else if (nums.length() == length) {
numStr += nums;
}
}
System.out.println(numStr + "," + length);
}
}
}