java--二进制字符串匹配的问题
2013-06-15 13:44
381 查看
描述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 thepattern A appeared at the posit题目大意:输入两个字符串A,B,只包含0或者1,有多少次A出现在B的字符串中,例如B是1001110110,A是11,有3次A出现在B中输入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. Andit 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.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt();//n=3 String str; String pattern; //循环3次 while(n > 0){ pattern = sc.next();//11 str = sc.next();//1001110110 int index = 0;//记录pattern在str中出现的索引 int count = 0;//记录出现的次数,默认=0 while(index != -1){ index = str.indexOf(pattern);//index = 3 if(index != -1){ //说明存在pattern count ++ ;//count = 1 str = str.substring(index+1);//此时str = 110110 } } System.out.println(count); n--; } } }
相关文章推荐
- java实现字符串匹配问题之求两个字符串的最大公共子串
- java实现字符串匹配问题之求两个字符串的最大公共子串
- 字符串匹配(Java)
- Java 正则表达式 字符串匹配
- Java实现算法导论中Rabin-Karp字符串匹配算法
- java 字符问题
- 全角字符Unicode编码及解问题中用到的Java基础知识
- 字符全排列问题--java
- java_正则表达式pattern类Matcher类_(字符串匹配)
- Java正则表达式 重复字符(串)问题
- Java正则表达式 重复字符(串)问题
- 第32章:字符串匹配问题: 朴素算法,Rabin-Karp算法
- java开发webservcie时关于特殊字符的问题
- JAVA:二进制(原码 反码 补码),位运算,移位运算,约瑟夫问题(5)
- java读取UTF-8的txt文件发现开头的一个字符问题
- 深入浅出Java中文问题(二)字符,字节和编码
- Java字符串split分割星号*等特殊字符问题(转)
- java 解决全站字符乱码(POST和GET中文编码问题)
- 算法实现字符串匹配问题:从三个字符串中找出共有的最长部分。
- java查询功能输入**中文字符乱码**问题: