输入一个字符串,返回其最长的数字子串,以及其长度。若有多个最长的数字子串,则将它们全部输出(按原字符串的相对位置)
本题含有多组样例输入。
数据范围:字符串长度 , 保证每组输入都至少含有一个数字
输入一个字符串。1<=len(字符串)<=200
输出字符串中最长的数字字符串和它的长度,中间用逗号间隔。如果有相同长度的串,则要一块儿输出(中间不要输出空格)。
abcd12345ed125ss123058789 a8a72a6a5yy98y65ee1r2
123058789,9 729865,2
样例一最长的数字子串为123058789,长度为9 样例二最长的数字子串有72,98,65,长度都为2
//一维动态规划解法 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); } } }
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(); System.out.println(maxNum(str)); } } private static String maxNum(String str) { //使用空格替换所有的字符,仅留下数字 String s = str.replaceAll("[A-Za-z]"," "); //根据空格拆分出数字串 String[] split = s.split(" "); String max = ""; StringBuilder sb = new StringBuilder(); for (String value: split) { //找到最长的数字串 if(value.length() > max.length()){ max = value; //将max保存到sb中 sb = new StringBuilder(max); }//数字串长度相等,就直接保存到sb中 else if (value.length() == max.length()){ sb.append(value); } } //最后保存逗号和最长数字串的长度 sb.append(",").append(max.length()); return sb.toString(); } }
import java.util.Scanner; import java.util.*; import java.util.Scanner; import java.util.regex.Pattern; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { String str = scanner.nextLine(); List<String> list = new ArrayList<>(); int len = 0; Pattern pattern = Pattern.compile("[0-9]+"); int k = str.length(); for (int i = 0; i < k; i++) { for (int j = k; j > i; j--) { String tmp = str.substring(i, j); if (pattern.matcher(tmp).matches()) { if (tmp.length() > len) { len = tmp.length(); list.clear(); list.add(tmp); } else if (tmp.length() == len) { list.add(tmp); } break; } } } String result = ""; for (String strr : list) { result += strr; } System.out.println(result + "," + len); } } }
import java.util.ArrayList; import java.util.List; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextLine()) { // 获取字符串 String str = in.nextLine(); // 将字符串中的数字字串拆分到数组中 String[] numStrs = str.split("[^0-9]+"); // 定义一个集合,用于存储字符串中最大的数字字串 List<String> maxNumList = new ArrayList<>(); // 定义一个变量,用于表示最长数字字串的最大长度 int maxLength = 0; // 通过循环查找最大的数字字串 for (int i = 0; i < numStrs.length; i++) { if (numStrs[i].length() > maxLength) { // 先清空 maxNumList.clear(); // 最大字串添加进入列表 maxNumList.add(numStrs[i]); // 求最大长度 maxLength = numStrs[i].length(); } else if (numStrs[i].length() == maxLength) { maxNumList.add(numStrs[i]); maxLength = numStrs[i].length(); } } // 输出最大数字字串结果 maxNumList.forEach(p-> { System.out.print(p); }); // 输出长度 System.out.println("," + maxLength); } } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while(in.hasNext()){ String[] strArr=in.nextLine().split("[^0-9]+"); // Arrays.sort(strArr); ArrayList<String> list=new ArrayList<>(); String str=""; for(int i=0;i<strArr.length;i++){ if(strArr[i].length()>str.length()){ list.clear();//当前串大于原最大串,先清除list。再将串添加到集合 str=strArr[i]; list.add(strArr[i]); }else if(strArr[i].length()==str.length()){ list.add(strArr[i]); } } for(String s:list){ System.out.print(s); } System.out.println(","+list.get(list.size()-1).length()); } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while(in.hasNextLine()){ String str = in.nextLine(); String[] newstr = str.split("[^0-9]+"); //正则 Arrays.sort(newstr,new Comparator<String>(){ //自定义排序 @Override public int compare(String o1,String o2){ return o2.length()-o1.length(); } }); String result=""; for(int i=0;i<newstr.length;i++){ if(newstr[i].length()==newstr[0].length()) result+=newstr[i]; } System.out.println(result+","+newstr[0].length()); } } }