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;
} }
UVA 814 The Letter Carrier's Rounds(JAVA基础map)的更多相关文章
- UVA 814 The Letter Carrier's Rounds
大致翻译: 对于电子邮件应用程序,您需要描述发生在成对mta之间的基于smtp的通信.发送方 的用户代理向发送消息传输代理(MTA)提供格式化的消息.发送MTA使用SimpleMail 传输协议(SM ...
- java基础-Map集合
java基础-Map集合 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Map集合概述 我们通过查看Map接口描述,发现Map接口下的集合与Collection接口下的集合,它 ...
- Java 基础 Map 练习题
第一题 (Map)利用Map,完成下面的功能: 从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队.如果该 年没有举办世界杯,则输出:没有举办世界杯. 附:世界杯冠军以及对应的夺冠年 ...
- java基础-Map的静态初始化以及Map的遍历等.....................
1.map的静态初始化,以及map遍历的几种方法: package com.cy.test; import java.util.HashMap; import java.util.Iterator; ...
- java基础-Map
简介 Map是一种接口,实现类有 hashMap SortedMap是继承自Map的接口,实现类为TreeMap,在内部会对Key进行排序 遍历Map 使用for each循环遍历Map实例的keyS ...
- The Letter Carrier's Rounds(摘)
Description For an electronic mail application you are to describe the SMTP-based communication that ...
- java基础——Map集合
Map以键值对的形式存储数据,其中Map.entry,是Map的内部类,它用来描述Map中的键值对.Map是一个接口,HashMap是他的一个实现类 Map中有几个重要的方法: get(Object ...
- Java基础 - Map接口的实现类 : HashedMap / LinkedHashMap /TreeMap 的构造/修改/遍历/ 集合视图方法/双向迭代输出
Map笔记: import java.util.*; /**一:Collection接口的 * Map接口: HashMap(主要实现类) : HashedMap / LinkedHashMap /T ...
- Java基础Map接口+Collections工具类
1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...
随机推荐
- 剑指Offer——矩阵中的路径
题目描述: 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子.如果一条路径经过了矩阵 ...
- 如何让thrift0.9.2 在macos上面编译通过?
为将来跨语言通信预研,选择了thrift来试试.结果在mac os上面安装遇到种种困难,不知道是我选择方法错误还是咋的,不管怎样,总算是编译过去了. 首先,我们来参考官网的安装步骤:https://t ...
- vim 设置字体和解决乱码
在 C:\Program Files (x86)\Vim 目录中的 _vimrc 文件中加入下面两行 set fileencodings=utf-8,gb2312,gb18030,gbk,ucs-bo ...
- python web框架 django 练习1 django 1.11版本
django练习 在我自己项目里创建一个xiaoliu的文件夹 里面创建s1.py 文件 s1.py文件 里面写各种函数 from django.shortcuts import HttpRespon ...
- Sails 框架学习资料
一介布衣 http://yijiebuyi.com/so.html?k=sails sails modules 模型自带的方法介绍 2016-09-06 929 nodejs查看更多 node.j ...
- 如何保护自己的GitHub代码不被别人覆盖
我们在自己的github上创建了免费的公开代码,为了防止别人通过git push upstream master 覆盖了自己原有的代码,需要作一下设置:Settings->Branchs,然后在 ...
- BC#29A:GTY's math problem(math) B:GTY's birthday gift(矩阵快速幂)
A: HDU5170 这题让比较a^b与c^d的大小.1<=a,b,c,d<=1000. 显然这题没法直接做,要利用对数来求,但是在math库中有关的对数函数返回的都是浮点数,所以这又要涉 ...
- springBoot 整合 RabbitMQ 的坑
1.Consumer raised exception, processing can restart if the connection factory supports it. Exception ...
- Portal系统中当切换学生时仍旧停留在当前页面的实现方法
一.BaseController.cs文件 1.OnActionExecuting方法,该方法可以被各子Controller重写 protected override void OnActionExe ...
- C#中获取时间差
/// <summary> /// 已重载.计算两个日期的时间间隔,返回的是时间间隔的日期差的绝对值. /// </summary> /// <param name=&q ...