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. Linux新手应掌握的10个基本命令

    导读 Linux对我们的生活有着很大的影响.然而在Linux上,你通常应该使用终端命令,而不是只要点击启动器图像(就像你在Windows上操作那样).这10个基本的Linux命令和重要命令会帮助你尽快 ...

  2. JavaScript深入浅出4-对象

    慕课网教程视频地址:Javascript深入浅出 对象的结构:包含一系列无序的属性,每个属性都有字符串key和对应的值 创建对象:对象字面量.new/原型链.Object.create 对象的属性操作 ...

  3. ndk学习15: IPC机制

    Linux IPC机制 来自为知笔记(Wiz)

  4. Droid4x快照还原

    一.问题描述 1. Droid4x还原快照可以通过VirtualBox 先还原快照 2. virtualbox 还原快照之后 如果没有用virtualbox启动 并关闭     而是直接启动Droid ...

  5. intellij Idea快捷键

    CTRL+ALT+O  优化导入的类和包 Alt + Center  导入类,实现接口 CTRL+N   查找类CTRL+SHIFT+N  查找文件CTRL+SHIFT+ALT+N 查找类中的方法或变 ...

  6. iOS_隐藏顶部状态栏方式

    关键词:IOS.UIViewController. Status Bar iOS6和iOS7在隐藏 Status Bar 三种方式比较: Storyboard 界面上选中UIViewControlle ...

  7. Wince常见操作

    1.获取本地代码启动路径 //在代码启动路径下存在 Files 文件夹 Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName(). ...

  8. 查看Android应用包名package和入口activity名称

    使用android自动化测试工具启动应用时,需要填写被测程序的包名和启动的Activity,以下有两种查看应用包名package和入口activity名称的方法: 方法一:使用aapt    //aa ...

  9. Unity3d《Shader篇》法线贴图

    效果图 贴图 法线贴图 //代码 Shader "Custom/NormalMap" { Properties { _MainTex ("Texture", 2 ...

  10. MPlayer-2016-bin-noConsole

    运行 Install-RMenu.cmd 添加右键播放功能 ; 往前0.05秒 大概10多个帧 ' 往后0.05秒 大概10多个帧 鼠标右键 快速定位 鼠标中键 退出 F1 缩小 F2 原始大小 F3 ...