题解:就是按照题目模拟就好

   但是这个题目让我发现了我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)的更多相关文章

  1. UVA 814 The Letter Carrier's Rounds

    大致翻译: 对于电子邮件应用程序,您需要描述发生在成对mta之间的基于smtp的通信.发送方 的用户代理向发送消息传输代理(MTA)提供格式化的消息.发送MTA使用SimpleMail 传输协议(SM ...

  2. java基础-Map集合

    java基础-Map集合 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Map集合概述 我们通过查看Map接口描述,发现Map接口下的集合与Collection接口下的集合,它 ...

  3. Java 基础 Map 练习题

    第一题 (Map)利用Map,完成下面的功能: 从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队.如果该 年没有举办世界杯,则输出:没有举办世界杯. 附:世界杯冠军以及对应的夺冠年 ...

  4. java基础-Map的静态初始化以及Map的遍历等.....................

    1.map的静态初始化,以及map遍历的几种方法: package com.cy.test; import java.util.HashMap; import java.util.Iterator; ...

  5. java基础-Map

    简介 Map是一种接口,实现类有 hashMap SortedMap是继承自Map的接口,实现类为TreeMap,在内部会对Key进行排序 遍历Map 使用for each循环遍历Map实例的keyS ...

  6. The Letter Carrier's Rounds(摘)

    Description For an electronic mail application you are to describe the SMTP-based communication that ...

  7. java基础——Map集合

    Map以键值对的形式存储数据,其中Map.entry,是Map的内部类,它用来描述Map中的键值对.Map是一个接口,HashMap是他的一个实现类 Map中有几个重要的方法: get(Object ...

  8. Java基础 - Map接口的实现类 : HashedMap / LinkedHashMap /TreeMap 的构造/修改/遍历/ 集合视图方法/双向迭代输出

    Map笔记: import java.util.*; /**一:Collection接口的 * Map接口: HashMap(主要实现类) : HashedMap / LinkedHashMap /T ...

  9. Java基础Map接口+Collections工具类

    1.Map中我们主要讲两个接口 HashMap  与   LinkedHashMap (1)其中LinkedHashMap是有序的  怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...

随机推荐

  1. 微信iOS版更新:可批量管理不常联系的朋友

    iOS版微信更新了v6.5.13版本,在新版本当中微信新增加了可批量管理不常联系的朋友功能,同时在群资料页可以查看最近收到的小程序,不过据网友爆料,腾讯在新的更新日志当中已经删除了“批量管理不常联系的 ...

  2. Jmeter(九)压力测试

    一般我们在做压力测试的时候,分单场景和混合场景,单场景也就是咱们压测单个接口的时候,多场景也就是有业务流程的情况下,比如说一个购物流程,那么这样的场景就是混合场景,就是有多个接口一起来做操作.1.单场 ...

  3. Jmeter(八)Jmeter监控tomcat

    1.配置tomcat的配置文件conf/tomcat-users.xml 2. 在“线程组”上右键“添加”--“配置元件”--“HTTP授权管理器”,这里主要是让JMeter能够通过Tomcat的基本 ...

  4. Linux下多个.c文件的编译和Makefile文件

    在编程的时候,我们可以把一个完整程序的每个函数分离出来,写成.c文件,最后再一起编译和链接.这样有利于程序功能模块化,也方便检查代码错误. .h文件:里面编辑该程序需要引用的头文件. #ifndef  ...

  5. TensorFlow学习笔记(六)循环神经网络

    一.循环神经网络简介 循环神经网络的主要用途是处理和预测序列数据.循环神经网络刻画了一个序列当前的输出与之前信息的关系.从网络结构上,循环神经网络会记忆之前的信息,并利用之前的信息影响后面节点的输出. ...

  6. linux automake使用

    一篇文章: 一.Makefile介绍 Makefile是用于自动编译和链接的,一个工程有很多文件组成,每一个文件的改变都会导致工程的重新链接,但是不是所有的文件都需要重新编译,Makefile中纪录有 ...

  7. docker——安装

    Docker划分为CE和EE.CE即社区版(免费,支持后期三个月),EE即企业版,强调安全,付费使用. #安装依赖包 yum install -y yum-utils device-mapper-pe ...

  8. bootstrap模态框嵌套、tabindex属性、去除阴影

    模态框嵌套 在开发中,遇到需要通过点击事件触发第一个模态框,触发后通过事件唤起第二个模态框,并且通过事件触发第三个模态框:即模态框嵌套. 模态框嵌套需要用一个模态框包裹所涉及嵌套的模态框,从而点击触发 ...

  9. The 15th UESTC Programming Contest Preliminary M - Minimum C0st cdoj1557

    地址:http://acm.uestc.edu.cn/#/problem/show/1557 题目: Minimum C0st Time Limit: 3000/1000MS (Java/Others ...

  10. Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) B. Code For 1

    地址:http://codeforces.com/contest/768/problem/B 题目: B. Code For 1 time limit per test 2 seconds memor ...