UVA 814 The Letter Carrier's Rounds(JAVA基础map)
题解:就是按照题目模拟就好
但是这个题目让我发现了我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; } }
分类:
ACM_模拟
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决