UVA 814 The letter Carrier ' s Rounds (Java base map)

Source: Internet
Author: User
Tags rounds

Problem solving: Just follow the questions and simulate.

But this topic has made me discover a lot of problems in Java.

Specific look at the code, but still divided into these aspects

Attribute scope problem, buffer problem, map and list mapping problem, output multiple spaces is not necessarily/t, inverse thought code optimization

Importjava.util.ArrayList;ImportJava.util.HashMap;ImportJava.util.HashSet;Importjava.util.List;ImportJava.util.Map;ImportJava.util.Scanner;ImportJava.util.Set; Public classmain{//map is an interface that cannot be instantiated, so you need to instantiate HashMap    Staticmap<string, list<string>> map =NewHashmap<string, list<string>>(); //multiple classes to use scanner, can not be built in each class itself, or the buffer will be problematic    StaticScanner sc =NewScanner (system.in);  Public Static voidMain (string[] args) { while(Sc.hasnext ()) {map.clear ();  while(true) {String type=Sc.next (); if("*". Equals (Type) {                     while(!" *". Equals (Solve ()));  Break;                } String email, name; intnum; Email=Sc.next (); Num=Sc.nextint (); //each time the list is refactored, the values in the previous or the map will be emptied.list<string> list =NewArraylist<string>();  while(num > 0) {Name=Sc.next (); //This is where you store your e-mail address instead of just the name, and you can compare it directly later.List.add (name+ "@" +email); --num;            } map.put (email, list); }        }    }    Private Staticstring Solve () {string sender; String[] Recipient=Newstring[100000]; Sender=Sc.next (); if("*". Equals (sender)returnSender; Set<String> set=NewHashset<string>(); recipient[0] =Sc.next (); Set.add (recipient[0]); intCoun = 0;  while(!" *". Equals (Recipient[coun])) {recipient[++coun] =Sc.next (); if(Set.contains (Recipient[coun])) {Coun--; }Else{set.add (Recipient[coun]); }        }        //Remove Carriage returnSc.nextline (); String Data= ""; String Temp=Sc.nextline ();  while(!" *". Equals (temp)) {            //Add all the rows to the datadata=data+ "" +temp+ "\ n"; Temp=Sc.nextline (); }        int[] Vis =New int[100000];  for(inti=0;i<vis.length;++i) vis[i]=0;  for(inti = 0; i < Coun; ++i) {if(Vis[i] = = 0) {String sendername=Las (sender); String recipientname=Las (Recipient[i]); System.out.println ("Connection between" + SenderName + "and" +recipientname); System.out.println ("HELO" +sendername); System.out.println ("250"); System.out.println ("MAIL from:<" + sender + ">"); System.out.println ("250"); intFlag = 0;  for(intj = i; J < Coun; ++j) {if(Vis[j] = = 0 && recipientname!=NULL&& recipientname.length ()!=0&&recipientname.equals (Las (recipient[j))) {Vis[j]= 1; System.out.println ("RCPT to:<" + recipient[j] + ">"); if(Check (Map.get (recipientname), Recipient[j])) {System.out.println ("250"); Flag= 1; } Else{System.out.println ("550"); }                    }                }                if(Flag = = 1) {System.out.println ("DATA"); System.out.println ("354");                    System.out.print (data); System.out.println ("     ."); System.out.println ("250"); } System.out.println ("QUIT"); System.out.println ("221"); }        }        return NULL; }    Private Static BooleanCheck (list list, String com) {if(List = =NULL||list.isempty ())return false;  for(Object i:list) {if(com!=NULL&& com.length () >0&&com.equals ((String) i)) {                return true; }        }        return false; }    Private Staticstring Las (String string) {inti;  for(i = 0; i < string.length (); + +i) {Chartemp =String.charat (i); if(temp = = ' @ ')) {                ++i;  Break; }} String ans= "";  for(; i < string.length (); + +i) {ans+=String.charat (i); }        returnans; }}

UVA 814 The letter Carrier ' s Rounds (Java base map)

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.