Binary String Matching

来源:互联网 发布:dos命令和linux命令 编辑:程序博客网 时间:2024/05/22 02:09
描述
 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.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int n = sc.nextInt();List<Object> list = new ArrayList<Object>();for (int i = 0; i < n; i++) {Map<String, String> map = new HashMap<String, String>();String str1 = sc.next();String str2 = sc.next();map.put("str1", str1);map.put("str2", str2);list.add(map);}print(list);}private static void print(List<Object> list) {for (int i = 0; i < list.size(); i++) {@SuppressWarnings("unchecked")Map<String, String> map = ((Map<String, String>) list.get(i));String str1 = (String) map.get("str1");String str2 = (String) map.get("str2");match(str1, str2);}}static void match(String str1, String str2) {int i, count = 0;i = str2.indexOf(str1);// strB在strA中首次出现的位置while (i >= 0) {count++;i = str2.indexOf(str1, i + 1);}System.out.println(count);}}

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 uc打开网页很慢怎么办 京东手机号码无法登录怎么办 织梦系统网站没收录怎么办 电脑开机出现一堆乱码怎么办 电脑文件夹出现乱码打不开怎么办 电脑出现f1和f2怎么办 电脑中韩文内容显示乱码怎么办 入驻shopee没身份证怎么办 液相色谱柱柱压降低怎么办 c18色谱柱堵了怎么办 色谱柱进空气了怎么办 宫颈评分只有3分怎么办 淘宝鞋子售后退货商家拒收怎么办 退货申通cp原因怎么办 运费险赔的少怎么办 淘宝卖游戏账号恶意退款怎么办 淘宝账号体检虚拟违规怎么办 京东虚拟单被骗怎么办 网络公选课挂科怎么办 淘宝评论被商家关闭怎么办? 皇冠车钥匙丢了怎么办 皇冠行李箱钥匙丢了怎么办 淘宝主推产品扣两分怎么办 淘宝直通车宝贝排查下架怎么办 滴滴车龄超过8年怎么办 购物车超120了怎么办 没发货申请退款卖家不处理怎么办 淘宝卖家帐号被骗了怎么办 淘宝网下单忘了用返利网怎么办 该地域无法观看此直播怎么办 宝宝喝了有活虫的奶粉怎么办 淘宝商家店铺状态异常怎么办 淘宝购物提示买家信息错误怎么办 苹果淘宝占用空间大怎么办 苹果手机淘宝占内存太大怎么办 苹果手机淘宝图标找不到了怎么办 苹果手机看淘宝很卡怎么办 苹果手机淘宝忘了密码怎么办 苹果手机更新后淘宝打不开怎么办 淘宝买到苹果翻新机怎么办 淘宝网密码忘了怎么办