Binary String Matching

来源:互联网 发布:广联达计价软件套定额 编辑:程序博客网 时间:2024/05/24 01:45

这题我没有做出来,在Eclipse上可以运行,目测结果正确,但是通不过ACM的那个网站测试,我不清楚为什么。懂的人可以告诉我一下。我的程序写的不怎么好,我正在慢慢改正。

时间限制:3000 ms  |  内存限制:65535 KB

描述
Given two strings A and B, whose alphabet consist only ‘0’ and ‘1’. Your task is only to tell how many times does A appear as a substring of B? For example, the text string B is ‘1001110110’ while the pattern string A is ‘11’, you should output 3, because the pattern A appeared at the posit
输入
The first line consist only one integer N, indicates N cases follows. In each case, there are two lines, the first line gives the string A, length (A) <= 10, and the second line gives the string B, length (B) <= 1000. And it is guaranteed that B is always longer than A.
输出
For each case, output a single line consist a single integer, tells how many times do B appears as a substring of A.
样例输入
3
11
1001110110
101
110010010010001
1010
110100010101011
样例输出
3
0
3

import java.util.Scanner;//写在前面的话,JAVA没有2进制的数字表示方法,所以不能用移位符类似010110110>>1,所以只能用字符串的方式进行匹配public class Main {    public static void main(String[] args) {        // TODO Auto-generated method stub        Scanner in = new Scanner(System.in);// 调用Scanner方法,从控制台输入        int count = in.nextInt();// 输入组数        in.nextLine();// 去换行符        String matchString = "";// inString元素        String targetString="";        int[] outCount=new int[count];        for(int i=0;i<count;i++)        {            outCount[i]=0;        }        for (int i = 0; i < count; i++) {            matchString = in.nextLine();// 匹配字符串            targetString=in.nextLine();//被匹配的长字符串            for (int j=0;(j+matchString.length())<targetString.length();j++)            {                if(matchString.equals(targetString.substring(j,j+matchString.length())))//如果字符匹配上了,就可以加1了                {                    outCount[i]++;                }            }        }        //in.close();        for(int element:outCount)//输出每样的匹配次数        {            System.out.println(element);        }    }}
0 0