2021秋招阿里粑粑最新笔试题(发现非常爱考哈希表)

题目的意思:

给定一个数组中的几个数,一开始是不重复的,然后对每个位置的数据进行调整,问经过 几次调整后可以找到两个重复的数。

#include 
#include 
#include 

using namespace std;

int twoUnique(vector  res){
   unordered_map  mp;
   for(auto  x : res) mp[x]++;
   for(auto x:res){
       if(mp[x]==2){
          return true;
       }
   }
   return false;
}
int main()
{
//    vector  res;
//    int n;
//    cin >>n;
//    int x;
//    while(cin>>x){
//        res.push_back(x);
//    }
    vector  res={8,4,2};
    int day=0;
    while(!twoUnique(res)){
        day++;
    for(int j=0;j

 

 

#include 
#include  
#include  
#include  
#include  

using namespace std;
typedef pair PAIR;
int strIndex(string str,string sub)
{
    int pos, i, j, tmp;
    if(str == ""|| sub == ""|| str.length() < sub.length()) return -1;
    for(i = 0; i < str.length(); i++)
    {
    if(str[i] == sub[0])
    {
    for(pos = i, j = 0; j < sub.length() && pos < str.length(); pos++, j++)
    {
    if(str[pos] != sub[j]) break;
    }
    }
    if(j == sub.length()) return pos-sub.length();
    }
    return -1;
}
bool cmp_by_value(const PAIR& lhs, const PAIR& rhs) {
  return lhs.second < rhs.second;
}
int main()
{
//    string s = "nowqqwasdqcoder";
//    string t="qqq";
//    int pos=strIndex(s,t);
//    cout<  map;
    string s;
    vector strvec;

    vector  vec;
    int n;
    int res;
    cin>>s>>n;
    vector t(n);
    for(int i=0;i>t[i];
       strvec.push_back(t[i]);
       res=strIndex(s,strvec[i]);
       map.insert(make_pair(i,res));
    }
   vector map_vec;

   for(auto it=map.begin();it!=map.end();it++){
      if(it->second!=-1){
      map_vec.push_back(*it);
      }
   }
  sort(map_vec.begin(), map_vec.end(), cmp_by_value);
  vector::iterator it=map_vec.begin();
  cout<first;

   for(int i=it->second+strvec[it->first].size();i

 

你可能感兴趣的:(笔试刷题每日一题)