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 ...
随机推荐
- BZOJ4034——[HAOI2015]T2
1.题目大意:用一个数据结构支持树的点修改和子树修改.树上路径和 2.分析:树链剖分裸题 #include <cstdio> #include <cstdlib> #inclu ...
- Intent flag 与启动模式的对应关系
Activity有四种启动模式: 1.standard(标准) 2.singleTop 3.singleTask 4.singleInstance 标识某个Activity的启动模式,有 ...
- Java文件操作工具类(复制、删除、重命名、创建路径)
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...
- 关于linux中文乱码的问题。
公司让人在一台装有ubuntu14.04的机器上安装net-snmp,可是这台机器的设置很让人不喜.没关系,一个个解决它. 不能连接外网,得弄一个代理. 这个好说,在可以上外网的本机上安装squid工 ...
- mysql根据汉字拼音排序查询
例如现在有一张数据表label,字段为id.name,此表的字符集设置为gb2312,以gb2312_chinese_ci整理. 设置InnoDB引擎的字符集也为简体中文,且整理为gb2312_chi ...
- django xadmin 外键
style_fields = {'db栏位名称': "fk-ajax"} 实体关系: Account (*)-->(1) user 表单控件: 下拉框 美化用了selecti ...
- centos 随意截屏
yum install kdegraphics 菜单路径: applications(应用程序)/Graphics(图形图像)/KSnapshot
- 08.01 签到! js 作用域
js 作用域 : 1.js 没有块作用域 : for (var i = 0;i < 4; i++){ } alert(i) // i = 3 2.js 没有动态作用域: function f1( ...
- HttpServletRequest中得到各种信息
1.获得domain: StringBuffer url = request.getRequestURL(); String domain = url.delete(url.length() - re ...
- 解决 vs2010 联接sql 2005 时 报错未能加载文件或程序集“Microsoft.SqlServer.Management.Sdk.Sfc
http://blogs.msdn.com/b/sqlnativeclient/archive/2008/05/30/sqlncli-msi-for-sql-server-2008.aspx 关键是这 ...