Huawei OJ Beginner: Find the longest consecutive number string

Source: Internet
Author: User
Tags numeric split time limit

Describe

Sample output

Output 123058789, function return value 9

Output 54761, function return value 5

Interface description

Function Prototypes:

Unsignedint Continumax (char** poutputstr, char* intputstr)

Input parameters:
char* intputstr input string;

Output parameters:
char** Poutputstr: The longest consecutive number string, if the length of the longest consecutive number string is 0, an empty string should be returned, and an empty string should be returned if the input string is empty;

return value:
The length of the longest consecutive number string

Knowledge points Bit arithmetic
Run time limit 10M
Memory limit 128
Input

Enter a string.

Output

The longest numeric string in the output string and its length.

If the numeric string is empty, only 0 is output

such as Input:dadfsaf output:0

Sample input abcd12345ed125ss123058789
Sample output 123058789,9
Import Java.util.Scanner;
		public class main{public static void Main (string[] args) {Scanner sc = new Scanner (system.in);
		String input = Sc.next ();
		Int[] result = new int[input.length ()];
		Sc.close (); for (int i = 0; i < input.length (); i++) {if (Input.charat (i) >= ' 0 ' && input.charat (i) <= ' 9 ') {//Times
				The calendar determines whether the number int is length = 0; for (; i < input.length (); i++) {//If it is a number, start from this position to the back to traverse if (Input.charat (i) >= ' 0 ' && input.charat (i) &L  t;= ' 9 ') {length++;  When it is a number, the length +1} else {break;  Otherwise end-break traversal} result[i] = length;
		I is the position where the string ends, length is the number string length}}} int length = 0;
		int temp = 0;  for (int i = 0; i < result.length; i++) {if (Result[i] >= length) {length = Result[i];
			Find the longest number string and its end position temp = i;
		}} if (length = = 0) System.out.println (0);
			else {String output = input.substring (temp-length + 1, temp + 1);
		SYSTEM.OUT.PRINTLN (Output + "," + length); }
	}
	
	//Method Two: Use split mates with regular expressions to separate strings into arrays containing only numbers, this method is only suitable for input strings that contain only characters and numbers private static void find (String input) {string[] string = input
		. Split ("[A-za-z]");
		String temp = null;
		int length = 0;
				for (int i = 0; i < string.length; i++) {if (length <= string[i].length ()) {length = String[i].length ();
			temp = String[i];
		}} if (string.length = = 0) System.out.println (0);
	else System.out.println (temp + "," + length); }
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.