题解:就是按照题目模拟就好
但是这个题目让我发现了我Java里面许多问题
具体看代码,但是还是分为这几个方面
属性的作用域问题,缓冲区问题,map与list映射的问题,输出多个空格不一定是/t,反转思想代码优化
import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.Set; public class Main{ // Map为接口不能实例化,所以需要实例化HashMap static Map<String, List<String>> map = new HashMap<String, List<String>>(); // 多个类要用到 scanner,不能再每个类里面自己建立,否则缓冲区会出现问题 static Scanner sc = new Scanner(System.in); public static void main(String[] args) { while (sc.hasNext()) { map.clear(); while (true) { String type = sc.next(); if ("*".equals(type)) { while (!"*".equals(Solve())); break; } String email, name; int num; email = sc.next(); num = sc.nextInt(); // 每次重构list,否则会清空之前还是map里面的值 List<String> list = new ArrayList<String>(); while (num > 0) { name = sc.next(); //这儿直接存储邮件地址而不是只存name,后面就可以直接比较了 list.add(name+"@"+email); --num; } map.put(email, list); } } } private static String Solve() { String sender; String[] recipient = new String[100000]; sender = sc.next(); if ("*".equals(sender)) return sender; Set<String> set=new HashSet<String>(); recipient[0] = sc.next(); set.add(recipient[0]); int coun = 0; while (!"*".equals(recipient[coun])) { recipient[++coun] = sc.next(); if(set.contains(recipient[coun])){ coun--; }else{ set.add(recipient[coun]); } } //去除回车 sc.nextLine(); String data = ""; String temp=sc.nextLine(); while (!"*".equals(temp)) { //将所有行都加入data中 data=data+" "+temp+"\n"; temp=sc.nextLine(); } int[] vis = new int[100000]; for (int i=0;i<vis.length;++i) vis[i]=0; for (int i = 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"); int flag = 0; for (int j = 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 boolean Check(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 static String Las(String string) { int i; for (i = 0; i < string.length(); ++i) { char temp = string.charAt(i); if (temp == ‘@‘) { ++i; break; } } String ans = ""; for (; i < string.length(); ++i) { ans += string.charAt(i); } return ans; } }
UVA 814 The Letter Carrier's Rounds(JAVA基础map)
时间: 2024-11-15 01:45:57