第五题: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Problem5 { private static int matchTimes(String son, String mother) { int res = 0; int index = 0; while (index < mother.length()) { int t = mother.indexOf(son, index); if (t >= 0) { res ++; index = t + 1; } else { break; } } return res; } private static void solution(List<String> strs, String[] opers) { boolean[] valid = new boolean[strs.size()]; for (String oper: opers) { if (oper.startsWith("+")) { valid[Integer.parseInt(oper.substring(1)) - 1] = false; } else if (oper.startsWith("-")) { valid[Integer.parseInt(oper.substring(1)) - 1] = true; } else { int count = 0; String mother = oper.substring(1); for (int i = 0; i < strs.size(); ++ i) { if (valid[i]) { continue; } count += matchTimes(strs.get(i), mother); } System.out.println(count); } } } public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String line = reader.readLine(); int n = Integer.parseInt(line.trim().split(" ")[0]), k = Integer.parseInt(line.trim().split(" ")[1]); List<String> strs = new ArrayList<>(k); String[] opers = new String[n]; for (int i = 0; i < k; ++ i) { strs.add(reader.readLine().trim()); } for (int i = 0; i < n; ++ i) { opers[i] = reader.readLine().trim(); } solution(strs, opers); } }
点赞 评论

相关推荐

不愿透露姓名的神秘牛友
07-17 14:06
点赞 评论 收藏
分享
牛客34884196...:你期望薪资4-5k,那确实可以重生了,但很难在深圳活下去
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务