本文主要是介绍OJ-0828,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目
示例1:
输入:
5
/huawei/computing/no/one
/huawei/computing
/huawei
/huawei/cloud/no/one
/huawei/wireless/no/one
2 computing
输出:
2
说明:在第二层级上,computing出现了2次,因此输出2
示例2:
输入:
5
/huawei/computing/no/one
/huawei/computing
/huawei
/huawei/cloud/no/one
/huawei/wireless/no/one
4 two
输出:
0
说明:存在第四层的URL上,没有出现two,因此频次是0
题解
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);int n = in.nextInt();in.nextLine();List<String> api = new ArrayList<>();for (int i = 0; i < n; i++) {api.add(in.nextLine());}int num = in.nextInt();String pathVal = in.next();int res = 0;for (String str : api) {String[] s = str.split("/");if (s.length - 1 < num) {continue;}if (s[num].equals(pathVal)) {res++;}}System.out.println(res);}
}
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);// 读取访问历史日志条数int count = Integer.parseInt(in.nextLine());// 读取每条日志String[] lines = new String[count];for (int i = 0; i < count; i++) {lines[i] = in.nextLine();}// 读取查询层级和关键字String[] search = in.nextLine().split(" ");int layer = Integer.parseInt(search[0]);String key = search[1];// 调用 doSearch 方法进行查询doSearch(lines, layer, key);}// 查询方法public static void doSearch(String[] paths, int layer, String key) {int count = 0;for (String path : paths) {// 按 / 分割路径String[] pp = path.split("/");// 判断路径长度是否足够,并且指定层级的字符串是否等于关键字if (pp.length >= layer + 1 && pp[layer].equals(key)) {count++;}}// 输出结果System.out.println(count);}
}
https://blog.csdn.net/weixin_52908342/article/details/135105720
这篇关于OJ-0828的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!