2 条题解

  • 0
    @ 2026-2-17 11:44:42

    这道题其实用 mapmapfindfind 函数就行,因为没有删除操作,只有增加操作。countcount 函数太慢了。

    #include<bits/stdc++.h>
    using namespace std;
    unordered_map<string,bool>mp;
    string op,str;
    inline void solve()
    {
    	cin>>op;getline(cin,str);
    	if(op[0]=='a')mp[str]=0;
    	else
    	{
    		if(mp.find(str)!=mp.end())puts("yes");
    		else puts("no");
    	}
    }
    int main()
    {
    	ios::sync_with_stdio(0);
    	cin.tie(0);cout.tie(0);
    	int n;cin>>n;
    	for(int i=1;i<=n;i++)solve();
    	return 0;
    }
    
    • 0
      @ 2025-10-8 16:53:05
      #include<bits/stdc++.h>
      using namespace std;
      
      unordered_map<string,int>mp;
      string op,str;
      int main()
      {
          int n;cin>>n;
          for(int i=1;i<=n;i++)
          {
              cin>>op;getline(cin,str);
              if(op[0]=='a') mp[str]++;
              else
              {
                  if(mp.count(str)>0)cout<<"yes"<<endl;
                  else cout<<"no"<<endl;
              }
          }
          return 0;
      }
      
      • 1

      *【STL:unordered_map】查找字符串(图书管理)

      信息

      ID
      66
      时间
      1000ms
      内存
      128MiB
      难度
      7
      标签
      递交数
      187
      已通过
      46
      上传者