NYOJ题目1051simone牌文本编辑器

----------------------------------------

题目评论中有说使用KMP反倒超时的。。。

外加看到匹配串长度不超过10,最大也不过是10000^10,这点计算量cpu表示没问题。

所以,就是用傻傻的模拟。。。。不愧是水题,竟然AC了。。。。

AC代码:

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4 
 5     public static void main(String[] args) {
 6         
 7         Scanner sc=new Scanner(System.in);
 8         
 9         int times=Integer.parseInt(sc.nextLine());
10         while(times-->0){
11             
12             String s1=sc.nextLine();
13             String s2=sc.nextLine();
14             
15             int ans=matches(s1,s2);
16             System.out.println(ans);
17             
18         }
19     }
20     
21     public static int matches(String s1, String s2){
22         int res=0;
23         for(int i=0,end=s1.length()-s2.length();i<=end;i++){
24             boolean isMatch=true;
25             for(int j=0;j<s2.length() && isMatch;j++){
26                 if(!(s1.charAt(i+j)==s2.charAt(j))) isMatch=false;
27             }
28             if(isMatch) res++;
29         }
30         return res;
31     }
32     
33 }

题目来源: http://acm.nyist.net/JudgeOnline/problem.php?pid=1051

原文地址:https://www.cnblogs.com/cc11001100/p/5875724.html