Binary String Matching

本文介绍了一个简单的Java程序,用于解决给定两个仅由'0'和'1'组成的字符串A和B时,如何计算字符串A作为子串在B中出现的次数。通过使用Java内置的indexOf方法进行遍历和匹配,程序能够高效地找到所有匹配项并计算总数。

描述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 

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4     public static void main(String[] args) {
 5         Scanner scanner=new Scanner(System.in);
 6         int T;
 7         String A;
 8         String B;
 9         int startPoint;
10         int count;
11         
12         T=scanner.nextInt();
13         while(T!=0){
14             T--;
15             startPoint=0;
16             count=0;
17             
18             A=scanner.next();
19             B=scanner.next();
20             
21             while(true){
22                 startPoint=B.indexOf(A,startPoint);
23                 
24                 if(startPoint==-1)
25                     break;
26                 
27                 count++;
28                 startPoint++;
29             }
30             
31             System.out.println(count);
32         }
33     }
34 }

 

转载于:https://www.cnblogs.com/zqxLonely/p/4129540.html

<think>我们按照要求将代码模块化。主要分为以下几个部分: 1. 输入验证和读取:在主函数中,读取输入并验证长度。 2. 将十六进制字符串转换为二进制字符串:我们可以写一个函数来完成。 3. 映射规则的管理:我们可以创建一个类来管理映射规则,并提供查询方法。 具体步骤: - 创建一个名为`OutputRuleMapper`的类,它包含一个`std::unordered_map`来存储规则,并提供: a. 构造函数:初始化默认规则(也可以后续添加) b. 一个`map`方法,根据二进制字符串查找对应的输出字符串,如果找不到则返回一个默认值(比如空字符串或者特殊标记)或者我们可以返回一个布尔值表示是否找到,这里我们采用返回字符串,找不到时返回空字符串,并在主函数中处理“未找到”的情况。 但是注意:原代码中,我们只处理8位的二进制字符串(即一个字节)。所以映射表也是8位二进制字符串到字符串的映射。 另外,原代码中,我们每次处理两个字符(一个字节)的十六进制字符串。 修改后的代码结构: 1. 主函数: 读取输入,验证长度。 创建`OutputRuleMapper`对象(并初始化规则)。 然后每两个字符(一个字节)进行处理: 将两个字符的十六进制字符串转换为8位二进制字符串。 调用`OutputRuleMapper`的映射函数,得到输出字符串。 根据映射结果输出信息。 2. 十六进制字符串转二进制字符串的函数:可以独立为一个工具函数。 3. `OutputRuleMapper`类:负责管理规则和映射。 注意:原代码中的映射规则只有5条,我们可以在类的构造函数中初始化这些规则。 另外,为了灵活性,我们还可以提供添加规则的方法,但根据当前需求,只需要在构造时初始化即可。 下面我们开始修改代码:</think>以下是修改后的代码,将功能模块化,主函数只负责输入验证和调用其他模块: ```cpp #include <iostream> #include <sstream> #include <bitset> #include <string> #include <unordered_map> class OutputRuleMapper { private: std::unordered_map<std::string, std::string> outputRules; public: OutputRuleMapper() { // 初始化映射规则 outputRules = { {"00000000", "."}, {"00000001", ".."}, {"00000010", "..."}, {"00000011", "...."}, {"00000100", "....."} // 可继续添加更多规则 }; } std::string getOutput(const std::string& binaryStr) { auto it = outputRules.find(binaryStr); return (it != outputRules.end()) ? it->second : ""; } }; std::string hexToBinary(const std::string& hexByte) { std::stringstream ss; ss << std::hex << hexByte; unsigned int byteValue; ss >> byteValue; return std::bitset<8>(byteValue).to_string(); } int main() { std::string input; std::cout << "请输入一个八字节的十六进制字符串(例如:123456789ABCDEF0): "; std::cin >> input; if (input.length() != 16) { std::cerr << "输入长度不正确,必须为16个字符(8字节)" << std::endl; return 1; } OutputRuleMapper mapper; for (size_t i = 0; i < input.length(); i += 2) { std::string hexByte = input.substr(i, 2); std::string binaryStr = hexToBinary(hexByte); std::string output = mapper.getOutput(binaryStr); std::cout << "Hex: " << hexByte << " -> Binary: " << binaryStr; if (!output.empty()) { std::cout << " -> Output: " << output << std::endl; } else { std::cout << " -> No matching rule found." << std::endl; } } return 0; } ``` ### 修改说明: 1. **`OutputRuleMapper`类**: - 封装映射规则管理 - 构造函数初始化规则映射表 - `getOutput()` 方法实现规则查询 2. **`hexToBinary`函数**: - 独立处理十六进制到二进制的转换 - 接受两个字符的十六进制字符串,返回8位二进制字符串 3. **主函数**: - 只负责输入验证和流程控制 - 每2个字符截取为字节单位 - 调用转换函数和映射类获取结果
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值