首页 > 试题广场 >

脸滚键盘

[编程题]脸滚键盘
  • 热度指数:4676 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

av394281 中,充满威严的蕾米莉亚大小姐因为触犯某条禁忌,被隙间妖怪八云紫(紫m……èi)按住头在键盘上滚动。
同样在弹幕里乱刷梗被紫姐姐做成罪袋的你被指派找到大小姐脸滚键盘打出的一行字中的第 `k` 个仅出现一次的字。
(
为简化问题,大小姐没有滚出 ascii 字符集以外的字)


输入描述:
每个输入都有若干行,每行的第一个数字为`k`,表示求第`k`个仅出现一次的字。然后间隔一个半角空格,之后直到行尾的所有字符表示大小姐滚出的字符串`S`。


输出描述:
输出的每一行对应输入的每一行的答案,如果无解,输出字符串`Myon~`

(请不要输出多余的空行)

为了方便评测,如果答案存在且为c,请输出[c]
示例1

输入

2 misakamikotodaisuki
3 !bakabaka~ bakabaka~ 1~2~9!
3 3.1415926535897932384626433832795028841971693993751o582097494459211451488946419191919l91919hmmhmmahhhhhhhhhh
7 www.bilibili.com/av170001
1 111

输出

[d]
[9]
[l]
[7]
Myon~

备注:
字符串S仅包含可见ascii码,长度不超过100000
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class Main {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		while (input.hasNext()) {
			int k = input.nextInt();
			String s = input.nextLine();
			Map<String, Integer> old = new LinkedHashMap<>(); // 出现过的字
			for (int i = 0; i < s.length(); i++) {
				if (!old.containsKey(s.substring(i, i + 1))) {
					old.put(s.substring(i, i + 1), 0);
				}
			}
			Set<String> keys = old.keySet();
			for (String str : keys) {
				for (int i = 0; i < s.length(); i++) {
					if (str.equals(s.substring(i, i + 1))) {
						int value = old.get(str);
						value++;
						old.put(str, value);				
					}
				}
			}
			int count = 0;
			for(String str:keys) {
				if(old.get(str)==1&&!str.equalsIgnoreCase(" ")) {
					count++;
				}
				if(count==k) {
					System.out.println("["+str+"]");
					break;
				}
			} 
			if(count<k||k<=0) System.out.println("Myon~");
		}
	}
} 
通过率为73%…………求解!
发表于 2019-09-10 01:53:56 回复(0)
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str;
        while ((str = br.readLine()) != null) {
            String num = str.split(" ")[0];
            int k = Integer.parseInt(num);
            String s = str.substring(num.length() + 1);
            System.out.println(appearFirstK(k, s));
        }
    }

    private static String appearFirstK(int k, String s) {
        char[] chars = s.toCharArray();
        int[] hash = new int[200];
        for (char c : chars) {
            hash[c]++;
        }
        for (char c : chars) {
            if (hash[c] == 1 && k == 1) {
                return "[" + c + "]";
            } else if (hash[c] == 1) {
                k--;
            }
        }
        return "Myon~";
    }
}
发表于 2019-08-20 16:39:11 回复(0)
java的循环输入真的坑
终于ac
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        ArrayList<String > strings = new ArrayList<>();
        int N=0;
        String s=null;

        while(scanner.hasNextLine()){
            s = scanner.nextLine();
            if (s.equals(""))break;
            String s1 = s.split("\\s")[0];
            N = Integer.parseInt(s1);
            strings.add(基数排序(N, s.substring(s1.length()+1)));
        }

        strings.forEach(s1 -> {
            if(s1.equals("Myon~")) System.out.println("Myon~");
            else System.out.println("["+s1+"]");
        });

    }

    private static String 基数排序(int n, String s) {
        int[] arr=new int[128];
        int count=0;
        for (int i = 0; i < s.length(); i++) {
            arr[s.charAt(i)]++;
        }
        for (int i = 0; i < s.length(); i++) {
            if (arr[s.charAt(i)]==1){
                count++;
                if (count==n) return String.valueOf(s.charAt(i));
            }
        }
        if (count<n||n<=0) return "Myon~";
        else return null;
    }
}

发表于 2019-08-20 13:29:59 回复(1)

热门推荐

通过挑战的用户

查看代码