(set)学单词

题目:

输入n,n行操作,每行有一个整数d和一个单词,d=0表示将单词存入集合,d=1表示测试这个单词是否在集合中,如果是,输出yes否则输出no。单词不区分大小写
样例输入:
5
0 we
0 are
1 family
0 Family
1 Family
样例输出:
No
Yes

分析与解答:

string要写在循环外面要不然超时。
这个题用了set的count函数判断元素否在集合里面

#include<iostream>
#include<cstdio>
#include<set>
#include<algorithm>

using namespace std;

int main(){
	set<string> c;
	int n;	int a;
	cin>>n;string k;
	while(n--){
	
		cin>>a>>k;
		int l=k.size();
		for(int i=0;i<l;++i){
			if('A'<=k[i]&&k[i]<='Z'){
				k[i]=k[i]-'A'+'a'; 
			}
		}
		if(a==0){
			c.insert(k);
		}
		else{
			if(c.count(k)){
				cout<<"Yes"<<endl;
			}
			else{
				cout<<"No"<<endl;
			}
		}
	}
	return 0;
}
发布了218 篇原创文章 · 获赞 131 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/qq_40828914/article/details/89047176
set