The purple book says that this question is a basic exercise for beginners (yes, it seems to be customized for me
After five or six days of training, I didn't do the question, so I chose this question with a recovery point mentality.
Who ever thought that not only did not recover, but also really gave me a blow
At first, I didn't understand the question very well, and then I understood the question with the input and output samples.
Because I haven't done the problem for a long time, I'm still looking forward to it, so I did it without thinking clearly.
It took me a whole day until 10 o'clock in the evening to work out the problem, and then RE came out.
Here is the code for RE
Train of thought:
Map < string, ArrayList > is used to store the effective place names and recipient information that are easy to query in the later stage.
Use ArrayList [] com = new ArrayList [dis. Length]; / / the recipient's information is stored in place names.
To judge
The error points are not carefully examined, the thinking is confused, and the conditions for when to process the data, when to read the data, and when to jump out of the cycle
As a result, it took a lot of time to fix and modify the code all the time, although the answer was barely correct, and the code was not beautiful and concise, and there were too few codes that could be directly used when the code was changed and modified in the later transformation of ideas.
public class UUva184{ public static void main(String[] args) { Scanner in = new Scanner(System.in); String after = in.next(); String next = after; Map<String, ArrayList<String>> maptolocal = new HashMap<>(); while(after.equals("MTA")) { next = after; //Save this time for comparison with the last time to determine whether it is time to quit /*Enter place name section*/ String local = in.next(); ArrayList<String> names = new ArrayList<>(); int num = in.nextInt(); for(;num>0;num--) { names.add(in.next()); } maptolocal.put(local, names); /*After processing, continue reading another place name or start typing letters.*/ after = in.next(); } /* Read the letter after completion*/ while(!(after.equals("*")&&next.equals("*"))) /* while(in.hasNext())*/ { /*after = in.next();*///Save this time for comparison with the last time to determine whether it is time to quit /*The first is the storage and processing of sender and receiver information*/ String line = ""; String t = ""; if(next.equals("MTA")||next.equals("*")) t = in.nextLine(); else { next += in.nextLine(); t = next; } if(t.equals("")) { line = in.nextLine(); } else { line = t; } /*Do it all separately*/ String[] dis = line.split(" "); String sendname = ""; //Name of sender String sendlocal = ""; //Mailing address String takename = ""; //name of addressee String takelocal = ""; //delivery address ArrayList[] com = new ArrayList [dis.length]; //The recipient's information is stored in place names for(int i =0;i<dis.length;i++) { int j = dis[i].indexOf('@'); com[i] = new ArrayList<>(); if(i!=0) { takename = dis[i].substring(0,j); takelocal = dis[i].substring(j+1); /*if(sendlocal)*/ boolean f = false; for( int c = 0;c<=i;c++) { if(com[c].contains(takelocal)) { /*It means there are duplicates, so this one doesn't need to be saved.*/ f = true; if(!com[c].contains(takename)) { com[c].add(takename); /*Indicates that there is a recipient in the same place*/ break; } /*Remove duplicate recipients*/ } } if(!f) { com[i-1].add(takelocal); com[i-1].add(takename); /*Otherwise, list this place separately. */ } } else //The first is the sender's message. { sendname = dis[i].substring(0,j); sendlocal = dis[i].substring(j+1); } } /*Start processing letters*/ //Read the letter String datum[] = new String[8]; after = in.next(); String da = in.nextLine(); String data = ""; if(da.equals("")) data = in.nextLine();//Specific contents of the letter else data = da; datum[0] = in.nextLine(); int a = 0; if(!datum[a].equals("*")) { while(!datum[a].equals("*")) { a++; datum[a] = in.nextLine(); } datum[a] = null; } /*output*/ for(int i=0;i<com.length;i++) { if(com[i].isEmpty()) continue; System.out.println("Connection between "+sendlocal+" and "+com[i].get(0)); System.out.println(" HELO "+sendlocal); System.out.println(" "+250); System.out.println(" MAIL FROM:<"+sendname+"@"+sendlocal+">"); System.out.println(" "+250); /* for(int k =0;k<com.length;k++)*/ boolean flag = false; for(int n =0;n<com[i].size()-1;n++) { if(maptolocal.containsKey(com[i].get(0))&&maptolocal.get(com[i].get(0)).contains(com[i].get(n+1))) { System.out.println(" RCPT TO:<"+com[i].get(n+1)+"@"+com[i].get(0)+">"); System.out.println(" "+250); flag = true; } else { System.out.println(" RCPT TO:<"+com[i].get(n+1)+"@"+com[i].get(0)+">"); System.out.println(" "+550); } } /*Data transmission officially started*/ if(flag) { System.out.println(" DATA"); System.out.println(" "+354); System.out.println(" "+data); if(!datum.equals("")) { for(int c =0;c<a;c++) System.out.println(" "+datum[c]); } System.out.println(" ."); System.out.println(" "+250); } System.out.println(" QUIT"); System.out.println(" "+221); } if(!after.equals("*"))/*It means that the contents of the letter are stored, so it is assigned as * because the contents of the letter have been read.*/ after = "*"; next = in.next(); } } }
AC Code:
1 use HashSet to store names and names
In the later stage, when the receiver information is identified, the stored combination is directly used for identification.
2 LinkedHashMap < string, integer > local
ArrayList[] names store multiple recipient names of corresponding addresses
Names * * [the address element in map is the VALUE corresponding to KEY] * *, which is stored in
Name
3 set to judge whether the current recipient information is added repeatedly
public class Uva184{ private static final boolean String = false; public static void main(String[] args) { Scanner in = new Scanner(System.in); String info = in.next(); //Mark the beginning of data storage HashSet<String> addr = new HashSet<>(); //Combined storage of place name and name int localsum = 0; while(!info.equals("*")) { String temp = in.next(); localsum++; int count = in.nextInt(); while(count>0) { addr.add(in.next()+"@"+temp); //In combination count--; } info = in.next(); } /*Who started reading letters*/ String sendname =""; String sendlocal =""; String takename =""; String takelocal =""; LinkedHashMap<String,Integer> Local = new LinkedHashMap(); ArrayList[] names = new ArrayList[localsum+1] ; //Find the corresponding A array based on the order of address elements Set<String> repeat = new LinkedHashSet<>(); //Judge whether it is repeated info = in.next(); //It's the first sender's Info. while(!info.equals("*")) { /*Split from @ without function*/ Local.clear(); //Data cleaning will be affected by previous data otherwise repeat.clear(); sendname = info.substring(0,info.indexOf("@")); sendlocal= info.substring(info.indexOf("@")+1); //The extracted string does not include@ int count = 0; /*Start reading the recipient's information and embed a while*/ /**Indicates that the recipient's information has been read and the content information has been read.*/ while(!info.equals("*")) { info = in.next(); if(info.equals("*")) break; takename = info.substring(0,info.indexOf("@")); takelocal = info.substring(info.indexOf("@")+1); //The extracted string does not include@ if(repeat.contains(info)) continue; //If the instructions are repeated, the place names and names will not be saved. repeat.add(info); if(!Local.containsKey(takelocal)) { Local.put(takelocal,count); names[count] = new ArrayList<>(); names[count].clear(); names[count].add(takename); count++; } else { /*Repeated ones have been excluded in the front, indicating that this must exist.*/ names[Local.get(takelocal)].add(takename); } } /*Now read the message*/ info = in.nextLine(); String[] data = new String[1000] ; int count2 = 0; if(info.equals("")) info = in.nextLine(); while(!info.equals("*")) { data[count2] = info; count2++; info = in.nextLine(); } /*Process after reading*/ /*output*/ Set a = Local.entrySet(); for(Map.Entry<String,Integer> entry: Local.entrySet()) { Integer takeindex = entry.getValue(); String takesite = entry.getKey(); /*System.out.println("Key: "+ entry.getKey()+ " Value: "+entry.getValue());*/ System.out.println("Connection between "+sendlocal+" and "+takesite); System.out.println(" HELO "+sendlocal); System.out.println(" "+250); System.out.println(" MAIL FROM:<"+sendname+"@"+sendlocal+">"); System.out.println(" "+250); boolean flag = false; for(int i =0;i<names[takeindex].size();i++) { String com = names[takeindex].get(i)+"@"+takesite; System.out.println(" RCPT TO:<"+names[takeindex].get(i)+"@"+takesite+">"); if(addr.contains(com)) { System.out.println(" "+250); flag = true; } else { System.out.println(" "+550); } } // Data transmission officially started if(flag) { System.out.println(" DATA"); System.out.println(" "+354); if(!data.equals("")) { for(int c =0;c<count2;c++) System.out.println(" "+data[c]); } System.out.println(" ."); System.out.println(" "+250); } System.out.println(" QUIT"); System.out.println(" "+221); } info = in.next(); } } }
Summary:
Recently, I have learned software engineering: the more preparation work in the early stage, the less the cost of remedying the mistakes in the later stage.
I feel that this theory is also very useful for me to do the problem. Really don't worry, carefully examine the problem, clear your mind and then do it, or you will die when you change it!
There must be a proper data structure. It's really important. Many times I've learned how to use it blindly. Half of them find that it can't be used, and the efficiency is not high, which is really painful.
So study hard, study hard, don't use half a bucket of water.