现在的位置: 首页 > 综合 > 正文

第十题 字符串包含问题

2018年04月13日 ⁄ 综合 ⁄ 共 560字 ⁄ 字号 评论关闭

题目:假设这有一个各种字母组成的字符串A,和另外一个字符串B,字符串里B的字母数相对少一些。什么方法能最快的查出所有小字符串B里的字母在大字符串A里都有?

如果是下面两个字符串:

String 1: ABCDEFGHLMNOPQRS

String 2: DCGSRQPO

答案是true,所有在string2里的字母string1也都有。

//字符串包含问题
#include <iostream>
using namespace std;
int main()
{
	string str1="ABCDEFGHLMNOPQRS";  
	string str2="DCGSRQPOM";  
	int hash[26]={0};  	
	//memset(hash,0,sizeof(hash)/sizeof(int));
	int num=0;
	for (int i=0;i<str1.length();++i)
	{
		if (hash[str1[i]-'A']==0)
		{
			hash[str1[i]-'A']=1;
		}
	}
	num=str2.length();
	for (int j = 0; j < str2.length(); j++)
	{
		if (hash[str1[j]-'A']==1)
		{
			num--;
		}
	}
	if (num==0)
	{
		cout<<"yes"<<endl;
	}
	else
	{
		cout<<"no"<<endl;
	}
	return 0;
}

抱歉!评论已关闭.