华为机试---字符串匹配

这是一道华为公司的机试题目,要求实现一个函数,判断一个短字符串中的所有字符是否都包含在长字符串中。函数原型为boolIsAllCharExist,接收两个参数,分别代表短字符串和长字符串。

题目描述

题目标题:

判断短字符串中的所有字符是否在长字符串中全部出现

详细描述:

接口说明

原型:

boolIsAllCharExist(char* pShortString,char* pLongString);

输入参数:

    char* pShortString:短字符串

    char* pLongString:长字符串 


输入描述:

输入两个字符串。第一个为短字符,第二个为长字符。

输出描述:

返回值:

输入例子:
bc
abc
输出例子:
true
import java.util.ArrayList;

import java.util.List;
import java.util.Scanner;


public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
String short_string = scan.nextLine();
String long_string = scan.nextLine();
boolIsAllCharExist(short_string , long_string);
}//endwhile
scan.close();
}
private static void boolIsAllCharExist(String short_string , String long_string){
List<Character> short_list = new ArrayList<Character>();
List<Character> long_list = new ArrayList<Character>();
int short_length = short_string.length();
int long_length = long_string.length();
for(int i = 0 ; i < short_length ; i++){
short_list.add(short_string.charAt(i));
}
for(int i = 0 ; i < long_length ; i++){
long_list.add(long_string.charAt(i));
}
char temp_ch = '*';
int count = 0;
for(int i = 0 ; i < short_length ; i++){
temp_ch = short_list.get(i);
if(long_list.contains(temp_ch)){
count++;
}
}
if(count == short_length){
System.out.println(true);
}else{
System.out.println(false);
}
}
}



### 华为OD中的增强版 `strstr` 函数 #### 题目描述 题目要求实现一个增强版本的 `strstr` 函数,其功能是在源字符串中查找第一个匹配的目标字符串,并返回目标字符串首次出现位置相对于源字符串起始位置的偏移量。如果未找到,则返回 `-1`。此函数支持带有通配符模式的模糊查询。 #### 解题思路 为了处理带通配符的模糊查询,在遍历过程中需考虑多种情况: - 当前字符完全匹配- 使用通配符代替任意单个字符; - 处理连续多个通配符的情况; 对于每种编程语言的具体实现方式有所不同,下面分别给出 C++ 和 Python 的解决方案[^1]。 #### C++ 实现方案 ```cpp #include <iostream> #include <string> using namespace std; int enhancedStrstr(const string& haystack, const string& needle) { int m = haystack.size(), n = needle.size(); for (int i = 0; i <= m - n; ++i) { bool match = true; for (int j = 0; j < n && match; ++j) { if (!(haystack[i + j] == '?' || needle[j] == '?' || haystack[i + j] == needle[j])) { match = false; } } if (match) return i; } return -1; } // 主程序用于读取输入并调用上述方法打印结果 int main() { string s, p; cin >> s >> p; cout << enhancedStrstr(s, p); } ``` #### Python 实现方案 ```python def enhanced_strstr(haystack: str, needle: str) -> int: m, n = len(haystack), len(needle) for i in range(m - n + 1): matched = True for j in range(n): if not any([ haystack[i+j] == ch or needle[j] == '?' for ch in [haystack[i+j], '?'] ]): matched = False break if matched: return i return -1 if __name__ == "__main__": source_string = input().strip() pattern_string = input().strip() result = enhanced_strstr(source_string, pattern_string) print(result) ``` 在实际考环境中需要注意的是,华为 OD 采用 ACM 模式进行考核,因此考生不仅需要完成核心算法逻辑的设计与编码工作,还需要自行负责数据的输入/输出操作部分[^3]。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值