首页 > 试题广场 >

抄送列表

[编程题]抄送列表
  • 热度指数:3359 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
NowCoder每天要处理许多邮件,但他并不是在收件人列表中,有时候只是被抄送。他认为这些抄送的邮件重要性比自己在收件人列表里的邮件低,因此他要过滤掉这些次要的邮件,优先处理重要的邮件。
现在给你一串抄送列表,请你判断目标用户是否在抄送列表中。

输入描述:
输入有多组数据,每组数据有两行。

第一行抄送列表,姓名之间用一个逗号隔开。如果姓名中包含空格或逗号,则姓名包含在双引号里。总长度不超过512个字符。

第二行只包含一个姓名,是待查找的用户的名字(姓名要完全匹配)。长度不超过16个字符。


输出描述:
如果第二行的名字出现在收件人列表中,则输出“Ignore”,表示这封邮件不重要;否则,输出“Important!”,表示这封邮件需要被优先处理。
示例1

输入

Joe,Kewell,Leon
Joe
"Letendre, Bruce",Joe,"Quan, William"
William

输出

Ignore
Important!
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        while(scan.hasNext()){
            String name = scan.nextLine();
            int pos = 0;
            Set<String> s = new HashSet<>();
            while(pos < name.length()){
                if(name.charAt(pos) == '\"'){
                    int end = name.indexOf('\"',pos+1); //从pos+1位置往后查找第一个"
                    String tmp = name.substring(pos+1,end); //截取[pos,end)之间的字符串
                    s.add(tmp);
                    pos = end + 2;  //到下一个名字的开头首字符
                }else{
                    int end = name.indexOf(',',pos+1);
                    //已经查到了最后一个名字,不是以,结尾
                    if(end == -1){
                        end = name.length()-1;
                        s.add(name.substring(pos,end+1));
                        break;
                    }
                    String tmp = name.substring(pos,end);
                    s.add(tmp);
                    pos = end + 1;
                }
            }
            name = scan.nextLine();
            if(s.contains(name) == true){
                System.out.println("Ignore");
            }else{
                System.out.println("Important!");
            }
        }
    }
}

发表于 2023-02-18 15:48:11 回复(0)
判断字符串三种情况:
1. 已“xxx”开头 
2. 已 ' , ' 开头 
3. 已 xxx 开头
import java.util.Scanner;
import java.util.ArrayList;

public class Main{
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNextLine()) {
            String str = in.nextLine();
            String cur = in.nextLine();

            ArrayList list = new ArrayList();
            for (int i = 0; i < str.length(); i++) {
                String ret = "";
                char c = str.charAt(i);
                
                // 1. 判断是"xxx"的情况
                if (c == '"') {
                    i++;
                    while (i < str.length() && str.charAt(i) != '"') {
                        ret += str.charAt(i);
                        i++;
                    }
                } else if (c == ',') {
                    // 2. 判断是','打头的情况(可分两种)
                    i++;
                    if(str.charAt(i) == '"'){
                        // 2.1 ','号后面是"xxx"的情况
                        i++;
                        while (i < str.length() && str.charAt(i) != '"') {
                            ret += str.charAt(i);
                            i++;
                        }
                    }else {
                        // 2.2 ','后面是xxx的情况
                        while (i < str.length() && str.charAt(i) != ',') {
                            ret += str.charAt(i);
                            i++;
                        }
                    }
                } else {
                    // 3. 已xxx开头的情况
                    while (i < str.length() && str.charAt(i) != ',') {
                        ret += str.charAt(i);
                        i++;
                    }
                }
                list.add(ret);
            }
            
            if (list.contains(cur)) {
                System.out.println("Ignore");
            } else {
                System.out.println("Important!");
            }
        }
    }
}


发表于 2022-06-03 10:02:28 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String str = sc.nextLine();
            String target = sc.nextLine();
            char[] ch = str.toCharArray();
            int flg = 0;
            List<String> list = new ArrayList<>();
            String ret = "";
            for(int i = 0;i < ch.length;i++){
                if(flg == 0 && ch[i] == '"'){
                    flg = 1;
                    list.add(ret);
                    ret = "";
                }else if(flg == 1 && ch[i] == '"'){
                    flg = 0;
                    list.add(ret);
                    ret = "";
                } else if(flg == 1){
                    ret += ch[i];
                }else if(flg == 0 && ch[i] != ','){
                    ret += ch[i];
                }
            }
                list.add(ret);
            
            boolean temp = false;
            for(String ss : list){
                if(ss.equals(target)){
                    temp = true;
                    break;
                }
            }
            if(temp){
                System.out.println("Ignore");
            }else{
                System.out.println("Important!");
                
            }
        }
    }
}

编辑于 2022-05-19 17:21:18 回复(0)
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNextLine()) {
			String s = sc.nextLine();
			String name = sc.nextLine();
			char[] ch = s.toCharArray();
			List<String> list = new ArrayList<>();
			int flag = 0;
			String res = "";
			for (int i = 0; i < ch.length; i ++ ) {
				if(flag == 0 && ch[i] == '"') {
					flag = 1;
					list.add(res);
					res = "";
				} else if(flag == 1 && ch[i] == '"') {
					flag = 0;
					list.add(res);
					res = "";
				} else if(flag == 1) {
					res += ch[i];
				} else if(flag == 0 && ch[i] != ',') {
					res += ch[i];
				}
			}
			list.add(res);
			boolean isFinded = false;
			for (String string:list) {
				if(name.equals(string)) {
					isFinded = true;
					break;
				}
			}
			if(isFinded) System.out.println("Ignore");
			else System.out.println("Important!");
		}
	}
}

发表于 2016-10-13 23:28:50 回复(1)

问题信息

难度:
4条回答 9992浏览

热门推荐

通过挑战的用户

查看代码