输入为多组测试数据。 输入数据只有一行,包含2个字符串A和B,每个字符串后面有一个#字符标记(#不属于A或B),其中B字符串不为空,两个字符串长度均不超过1000000。 输出 如果B中的字符全部在A中出现,输出Yes,否则输出No。用C语言代码表示
时间: 2024-04-08 20:29:38 浏览: 65
可以使用一个哈希表来解决这个问题。首先,遍历字符串A,将每个字符及其出现的次数存储在哈希表中。然后,遍历字符串B,检查每个字符是否在哈希表中,并且字符出现的次数大于等于B中对应字符的次数。如果所有字符都满足条件,则输出"Yes";否则,输出"No"。
下面是用C语言实现的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 26
// 判断B中的字符是否全部在A中出现
int isAllCharsInA(char* A, char* B) {
int countA[MAX_SIZE] = {0}; // A中每个字符出现的次数
int lenA = strlen(A);
int lenB = strlen(B);
// 遍历字符串A,统计每个字符出现的次数
for (int i = 0; i < lenA; i++) {
countA[A[i] - 'a']++;
}
// 遍历字符串B,检查每个字符是否在A中出现
for (int i = 0; i < lenB; i++) {
if (countA[B[i] - 'a'] <= 0) {
return 0; // 字符不在A中出现
}
countA[B[i] - 'a']--;
}
return 1; // 所有字符都在A中出现
}
int main() {
char input[2000000]; // 输入字符串
char* A;
char* B;
while (fgets(input, sizeof(input), stdin) != NULL) {
// 分割输入字符串
A = strtok(input, "#");
B = strtok(NULL, "#");
if (isAllCharsInA(A, B)) {
printf("Yes\n");
} else {
printf("No\n");
}
}
return 0;
}
```
示例输入:
```
abcdefg#bcd
abc#xyz
```
示例输出:
```
Yes
No
```
阅读全文
相关推荐


















