每行一条输入,格式为 type:ip
type 包括 i d s 分别表示添加,删除,查找
以 end 结尾
输入最多不超过100000行
输出每行一条对应输入
如果是查找,成功请打印true,失败请打印false
如果是添加删除,请打印ok
i:127.0.0.1 i:10.0.0.1 s:10.0.0.1 d:10.0.0.1 s:10.0.0.1 s:127.0.0.1 end
ok ok true ok false true
i:127.0.0.3 i:127.0.0.3 d:127.0.0.4 s:127.0.0.3 i:127.0.0.5 d:127.0.0.5 s:127.0.0.4 i:127.0.0.4 s:127.0.0.3 d:127.0.0.4 end
ok ok ok true ok ok false ok true ok
import java.util.HashMap; import java.util.Scanner; /** * @Date: 2020-05-04 22:54 * @version: 1.0 */ public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); HashMap<String,Boolean> al = new HashMap<>(); while (true){ String s = sc.next(); if ("end".equals(s)) break; char op = s.charAt(0); String ip = s.substring(2, s.length()); if (op == 'i'){ al.put(ip,true); System.out.println("ok"); }else if (op == 'd'){ al.put(ip,false); System.out.println("ok"); }else { if (al.get(ip)==null || al.get(ip)==false) System.out.println(false); else System.out.println(true); } } } }
import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class Main { public static void main(String[] args) { Set<String> values = new HashSet<>(); String ip; String op; String str; Scanner cin = new Scanner(System.in); while (cin.hasNext()) { str = cin.next(); if (str.equals("end")) { return; } op = str.substring(0, 1); ip = str.substring(2); if (op.equals("i")) { values.add(ip); System.out.println("ok"); } if (op.equals("s")) { if (values.contains(ip)) { System.out.println("true"); } else { System.out.println("false"); } } if (op.equals("d")) { values.remove(ip); System.out.println("ok"); } } } }