Binary String Matching

 <div class="problem-display" style="font-size: 14px; color: rgb(70, 70, 70); font-family: Tahoma, Arial, sans-serif, simsun;"><h2 style="margin: 0px; padding: 0px; font-size: 18px; text-align: center; color: rgb(113, 32, 21); font-family: 微软雅黑, 黑体;">Binary String Matching</h2><div class="problem-ins" style="text-align: center;">时间限制:<span class="editable highlight" id="problem[time_limit]" style="color: rgb(113, 32, 21);">3000</span> ms  |  内存限制:<span class="editable highlight" id="problem[memory_limit]" style="color: rgb(113, 32, 21);">65535</span> KB</div><div class="problem-ins" style="text-align: center;">难度:<span class="editable highlight" style="color: rgb(113, 32, 21);">3</span></div></div><div class="clr" style="clear: both; color: rgb(70, 70, 70); font-family: Tahoma, Arial, sans-serif, simsun; font-size: 13px; line-height: 19px;"></div><dl class="problem-display" style="margin: 0px; padding: 0px; font-size: 14px; color: rgb(70, 70, 70); font-family: Tahoma, Arial, sans-serif, simsun;"><dt style="margin: 1em 0px 0.2em; padding: 0px; color: rgb(113, 32, 21); font-size: 16px; font-weight: bold;">描述</dt><dd style="margin: 0px; padding: 0px;">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</dd><div class="clr" style="clear: both;"></div><dl class="others" style="margin: 0px; padding: 0px;"><dt style="margin: 1em 0px 0.2em; padding: 0px; color: rgb(113, 32, 21); font-size: 16px; font-weight: bold;">输入</dt><dd style="margin: 0px; padding: 0px;">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.</dd><dt style="margin: 1em 0px 0.2em; padding: 0px; color: rgb(113, 32, 21); font-size: 16px; font-weight: bold;">输出</dt><dd style="margin: 0px; padding: 0px;">For each case, output a single line consist a single integer, tells how many times do B appears as a substring of A.</dd><dt style="margin: 1em 0px 0.2em; padding: 0px; color: rgb(113, 32, 21); font-size: 16px; font-weight: bold;">样例输入</dt><dd style="margin: 0px; padding: 0px;"><pre id="sample_input" style="margin-top: 0px; margin-bottom: 0px; padding: 5px 10px; font-family: Consolas, 'Courier New', 'DejaVu Sans Mono', 'Droid Sans Mono', monospace; background-color: rgb(239, 239, 239); border: 1px solid rgb(204, 204, 204); min-height: 20px; line-height: 1.5em;">3
11
1001110110
101
110010010010001
1010
110100010101011 
样例输出
3
0
3 


import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int num = scanner.nextInt(); for(int i =0;i<num;i++) { String str = scanner.next(); int index =0; int temp=0; String str2 = scanner.next(); while(index!=-1) { index= str2.indexOf(str); if(index!=-1) { temp++; str2 = str2.substring(index+1); } } System.out.println(temp); } }}
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值