import java.util.HashMap;
/* 判断短字符串中的所有字符是否在长字符串中全部出现
* 遍历长字符串,统计各个字符出现的频率
* 遍历短字符串的各个字符,在长字符该字符下频率是否为0,如果有为0的就说明不是全部存在,全部不为0,就是全部存在
* */
import java.util.Scanner;
public class Main {
public static boolean isAllCharExist(String shortString, String longString){
int[] bucket = new int[128];
for (int i = 0; i < longString.length(); i++) //桶 统计频率,某个字符出现频率不为0
bucket[longString.charAt(i)]++;
for (int i = 0; i < shortString.length(); i++) { //短字符串各个字符在长字符串各个字符频率情况
if(bucket[shortString.charAt(i)] == 0)
return false;
}
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String strShort = sc.nextLine();
String strLong = sc.nextLine();
System.out.println(isAllCharExists(strShort, strLong));
}
}
}
可以首先把长字符串中的每个字符依次放进map里面并且令其值为1,然后对短字符串中每个字符串看看对应的map是不是为1,如果不是直接退出返回false,短字符串中的每个字符对应的map值都为1返回true。算法复杂度为线性。
#include<iostream>
#include<string>
#include<map>
using namespace std;
bool IsAllCharExist(string str1,string str2)
{
int len1=str1.size(),len2=str2.size();
map<char,int>map;
for(int i=0;i<len2;++i)
map[str2[i]]=1;
for(int i=0;i<len1;++i)
if(map[str1[i]]!=1)return false;
return true;
}
int main()
{
string str1,str2;
while(cin>>str1>>str2)
{
if(IsAllCharExist(str1,str2))cout<<"true"<<endl;
else cout<<"false"<<endl;
}
return 0;
}
//思路:判断s1中的每个字符是否都在s2中
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){
string s1,s2;
while(cin>>s1>>s2){
bool flag=true;
for(int i=0; i<s1.size(); i++){
if(s2.find(s1[i])==-1){
cout<<"false"<<endl;
flag=false;
break;
}
}
if(flag)
cout<<"true"<<endl;
}
return 0;
}
//多定义一个字符串flag,第一个字符串中有与第二个字符串相同的字符时,
//将flag中相应的字符置为‘a’或其他都可以,最后判断flag与第一个字符串长度是否相同
#include<stdio.h>
#include<string.h>
int main()
{
char duan[100];
while(gets(duan)!=NULL)
{char chang[100];
char flag[100]={0};
gets(chang);
int i,j;
for(i=0;i<strlen(duan);i++)
{
for(j=0;j<strlen(chang);j++)
{
if(duan[i]==chang[j])
flag[i]='a';
}
}
if(strlen(duan)==strlen(flag))
printf("true\n");
else printf("false\n");
}
}
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main() {//第一次看成KMP了,算半天提交出错
string str1, str2;
while (cin >> str1 >> str2) {
vector<bool> helper(26, false);
bool flag = true;
for (auto s : str2) helper[s - 'a'] = true;
for (auto s : str1)
if (!helper[s - 'a']) {
flag = false; break;
}
if (flag) cout << "true" << endl;
else cout << "false" << endl;
}
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
string shortStr, longStr;
while(cin >> shortStr >> longStr)
{
bool flag = true;
int arr[26] = {0};
for(int i = 0; i < longStr.length(); i++)
arr[longStr[i]-'a']++; //记录出现的字符
for(int i = 0; i < shortStr.length(); i++)
{
if(!arr[shortStr[i]-'a'])
{
flag = false;
break;
}
}
cout << (flag? "true":"false") << endl;
}
return 0;
}
#include<string>
(765)#include<iostream>
#include<stdlib.h>
using namespace std;
string String_matching(string &str1, string &str2)
{
// 判断短字符串中的所有字符是否在长字符串中出现
for (int i = 0; i < str1.size(); i++)
{
if (str2.find(str1[i], 0) == string::npos)
{
return "false";
}
}
return "true";
}
int main()
{
string str1,str2;
while (cin >> str1 >> str2)
{
string ans = String_matching(str1, str2);
cout << ans << endl;
}
system("pause");
return 0;
} 解决方法:使用将长字符串放入unordered_set中,然后遍历段字符串寻找是否存在。
时间复杂度:O(s) + O(l) s:短字符串长度 l:长字符串长度
#include <string>
#include <iostream>
#include <unordered_set>
using namespace std;
int main(){
string a, b;
while(cin >> b >> a){//b为短字符串 a为长字符串
bool ans = true;
unordered_set<char> sa(a.begin(), a.end());
for(char c : b){
if(sa.find(c) == sa.end()){
ans = false;
break;
}
}
if(ans) cout << "true" << endl;
else cout << "false" << endl;
}
return 0;
}
/*
*** 本代码思路比较简单:char[]数组存放短字符串,遍历char[]数组,并使用string.contains()
* 方法判断长字符串是否含有短字符,如果全都含有,则打印true,判断过程中不含有某个短字符
* ,则打印false,并break;
*/
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
char[] shortChar = sc.nextLine().toCharArray();
String str = sc.nextLine();
for(int i=0;i<shortChar.length;i++){
if(!str.contains(String.valueOf(shortChar[i]))){
System.out.println("false");
break;
}
if(i == shortChar.length-1){
System.out.println("true");
}
}
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String shortS = br.readLine();
String longS = br.readLine();
int[] l = new int[26];
for(char c : longS.toCharArray()){
l[c-'a']++;
}
for(char c : shortS.toCharArray()){
if(l[c-'a']==0){
System.out.println(false);
return;
}
}
System.out.println(true);
}
}
因为是小写字母,所以直接用数组来保存就行
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String s = sc.nextLine();
String l = sc.nextLine();
System.out.println(match(s, l));
}
}
// 匹配短串字符是否出现在长串(不需按顺序)
public static boolean match(String s, String l) {
out : for (int i=0; i<s.length(); i++) {
for (int j=0; j<l.length(); j++) {
if (s.charAt(i) == l.charAt(j)) {
if (i == s.length()-1) {
break out;
}
continue out;
}
}
return false;
}
return true;
}
} import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while(in.hasNextLine()){
String s =in.nextLine();
String l =in.nextLine();
boolean flag = true;
for(int i=0;i<s.length();i++){
if(l.contains(String.valueOf(s.charAt(i)))){
continue;
} else {
flag = false;
}
}
System.out.println(flag);
}
}
} #include<bits/stdc++.h>
using namespace std;
int main(){
string a,b;
while(cin>>a>>b){
string f = "true";
for(int i = 0;i<a.size();i++){
if(b.find(a[i])==-1){
f = "false";
break;}
}
cout<<f<<endl;
}
} #include<iostream>
#include<stdio.h>
#include<string>
#include<algorithm>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<unordered_set>
#include<unordered_map>
using namespace std;
int main()
{
string a,b;
while(cin>>a>>b) {
bool fre[26];
memset(fre,0,sizeof(fre));
for(char ch:a) {
fre[ch-'a']=true;
}
for(char ch:b) {
fre[ch-'a']=false;
}
bool flag=true;
for(int i=0;i<26;++i) {
if(fre[i]) {
flag=false;
break;
}
}
if(flag)
cout<<"true"<<endl;
else
cout<<"false"<<endl;
}
return 0;
}