The first occurrence of a character in a string (Java)

Source: Internet
Author: User
Tags gz file

is challenging a crackme you, the need to fill in the first few passwords are correctly guessed, but this last password, as if hidden a bit deep. The author of the Crackme also provocative in hiding a. tar.gz file, extracted, and wrote the last character you want is in the string below. This character is the first occurrence of a character in the entire string below. (For example, the string is Abaccdeff, then the correct character is B) However, the strings given below appear to be too long and too long to be found by manpower alone. So, you need to write a program to do it. The input file volume is large, please use some fast input and output means, do not recommend the use of Cin/cout, Java is not recommended to use scanner direct read and write.

Input Description:
The first line, a positive integer T (t≤20), represents the number of input data groups. After the T line, each line is a string s. (The length of the 1≤s is ≤10 6, guaranteeing that the ASCII code of the characters appearing in the string is within the range of [0x21,0x7f), that is, non-whitespace characters that can be displayed, and that there must be a solution)



Output Description:
A total of T-lines, one character c per line, representing the first character in the given string that appears only once.


Input Example:
2abaccdefftestonline


Output Example:
Bs


Code implementation

As follows: Because the ASCII code of the characters appearing in the string is within the [0x21,0x7f] range, it contains a total of 95 characters, so the number of occurrences of a 95-size array for storing characters is requested.

import java.util.scanner;public class  main {public static void main (String[] args)  {// TODO  auto-generated method stubint i=0; Scanner sc = new scanner (system.in);if  (Sc.hasnext ())  {int num =  Sc.nextint (); while (I<num) {string str = sc.next (); find (str); i++;}}} /* * * find char */public static void find (STRING&NBSP;STR)  {char  strarray[] = str.tochararray ();int charnum[] = new int[95];for  (int  a : charnum)  {a = 0;} for  (Char s : strarray)  {charnum[s-'! '] =charnum[s-'! '] +1;} The first 1 characters in the list are queried in order for  (Int i=0;i<str.length (); i++)  {if  (Charnum[str.charat (i)-'! ')  == 1)  {system.out.println (Str.charat (i); break;}}}} 

The first occurrence of a character in a string (Java)

Related Article

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.