Binary String Matching

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

描述
    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

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

AC代码:

 import java.util.Scanner;

 public class Main {

     public static void main(String[] args) {

         Scanner sc=new Scanner(System.in);
int n=Integer.parseInt(sc.nextLine()); while(n-->0){
String a=sc.nextLine();
String b=sc.nextLine();
int ans=solve(a,b);
System.out.println(ans);
} } public static int solve(String a,String b){
int res=0;
for(int i=0;i<b.length();i++){
int j=0;
for(j=0;j<a.length();j++){
if((i+j>=b.length()) || (a.charAt(j)!=b.charAt(i+j))) break;
}
if(j==a.length()) res++;
}
return res;
} } 另一种AC思路:(利用了内建的方法,不重复造轮子) import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in);
int n=Integer.parseInt(sc.nextLine()); while(n-->0){
String a=sc.nextLine();
String b=sc.nextLine();
int ans=solve(a,b);
System.out.println(ans);
} } public static int solve(String a,String b){
int res=0;
while(a.length()<=b.length()){
if(b.indexOf(a)==0) res++;
b=b.substring(1,b.length());
}
return res;
} }

题目:http://acm.nyist.net/JudgeOnline/problem.php?pid=5

NYOJ之Binary String Matching的更多相关文章

  1. NYOJ 5 Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  2. nyoj 5 Binary String Matching(string)

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  3. nyoj 题目5 Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  4. Binary String Matching

    问题 B: Binary String Matching 时间限制: 3 Sec  内存限制: 128 MB提交: 4  解决: 2[提交][状态][讨论版] 题目描述 Given two strin ...

  5. ACM Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  6. Binary String Matching(kmp+str)

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  7. 【ACM】Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  8. NYOJ5——Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3  描述:Given two strings A and B, whose alph ...

  9. NYOJ5 Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

随机推荐

  1. HDU 3911 线段树区间合并、异或取反操作

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...

  2. Java 7 Concurrency Cookbook 翻译 第一章 线程管理之三

    五.睡眠和唤醒一个线程 有时,你会想要在一段特定的时间后再去中断线程的运行.举个例子,程序中的一个线程每一分钟检查一次传感器的状态,剩余的时间,线程应该处于空闲的状态.在这段空闲时间里,线程不会使用计 ...

  3. iTool拷贝app到电脑上

    iTool拷贝app到电脑上 方法一. iTool找到你的app, 归档在桌面, 桌面就生成了ipa, 其实ipa是一个压缩包, 使用解压软件解压之后 生成Payload文件夹, 点开就可以看到Clo ...

  4. python 深入模块和包

    模块可以包含可执行语句以及函数的定义. 这些语句通常用于初始化模块. 它们只在 第一次 导入时执行.只在第一次导入的时候执行,第一次.妈蛋的第一次...后面再次导入就不执行了. [1](如果文件以脚本 ...

  5. hiho #1114 : 小Hi小Ho的惊天大作战:扫雷·一

    #1114 : 小Hi小Ho的惊天大作战:扫雷·一 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 故事背景:密室.监视器与充满危机的广场 “我们还是循序渐进,先来考虑这 ...

  6. Ubuntu 中安装 NetBeans IDE

    NetBeans 8.2 刚刚发布,如果你还没有安装的话,这篇简短的教程将会演示如何在 Ubuntu 系统上快速的安装.对安装 NetBeans 需要帮助的开发者来说,这往篇文章会是不错的指导. 给那 ...

  7. 2.7---判断链表是否是回文(CC150)

    注意,如果用的方法是翻转整个链表,那么链表都被改变了.就无法做了. 此外注意fast.next.next!= null;不然也会报错.要保证后面的比前面的少. 答案: public static bo ...

  8. JavaScript——特殊点总结

    首先,== equality 等同,=== identity 恒等. ==, 两边值类型不同的时候,要先进行类型转换,再比较. ===,不做类型转换,类型不同的一定不等. 下面分别说明: 先说 === ...

  9. JavaScript——this、constructor、prototype

    this this表示当前对象,如果在全局作用范围内使用this,则指代当前页面对象window: 如果在函数中使用this,则this指代什么是根据运行时此函数在什么对象上被调用. 我们还可以使用a ...

  10. c#.net Excel中的数据导入到SQL数据库中

    /// <summary>        /// 从Excel 导入学生        /// </summary>        /// <param name=&qu ...