NYOJ之Binary String Matching
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的更多相关文章
- NYOJ 5 Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- nyoj 5 Binary String Matching(string)
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- nyoj 题目5 Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- Binary String Matching
问题 B: Binary String Matching 时间限制: 3 Sec 内存限制: 128 MB提交: 4 解决: 2[提交][状态][讨论版] 题目描述 Given two strin ...
- ACM Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- Binary String Matching(kmp+str)
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- 【ACM】Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- NYOJ5——Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述:Given two strings A and B, whose alph ...
- NYOJ5 Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
随机推荐
- 关于viewport
最近无聊的很,买了本教材,学习响应式网站设计. 因为有多年css的编程基础,前面的媒介查询学的很顺利.当学到viewport这个mata标签的时候,教程讲的比较简单. 今天,百度了不少资料,基本搞清楚 ...
- IO基础
流向: 输入流:从硬盘到java 程序 (读数据) 输出流:从java 到硬盘 (写数据) 数据类型: 字节流: {用记事本打开,不能读懂,用 字节流} 输入: InputStrea ...
- 【转载】javaAgent 参数
-javaagent 这个JVM参数是JDK 5引进的. java -help的帮助里面写道: -javaagent:<jarpath>[=<options>] load Ja ...
- windows程序是如何开始执行的??
windows的资源管理器侦测到使用者执行了一个程序————>windows调用加载器加载该程序————>调用C start code——>C start code 调用WinMai ...
- [BZOJ1662][POJ3252]Round Numbers
[POJ3252]Round Numbers 试题描述 The cows, as you know, have no fingers or thumbs and thus are unable to ...
- 瀑布流js排列
var _px = document.getElementById("px"); var con=document.getElementById("content&quo ...
- hdu1520 树形dp Anniversary party
A - Anniversary party Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I6 ...
- tornado + supervisor + nginx 的一点记录
看了比较多的blog基本都是这个架构: supervisor ------------ app1 |-------app2 |-------.... |-------appn |-------ngin ...
- sublime text 个性设置
http://stackoverflow.com/questions/13781833/sublime-text-2-how-to-change-the-font-size-of-the-file-s ...
- Activity中UI框架基本概念
Activity中UI框架基本概念 Activity 是应用程序的基本组成部分,提供了可视的界面,UI容器, 与用户进行交互: 具体Acitivity是怎么样显示这些事视图元素以及响应事件交互的. 一 ...