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

题目的意思:

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

#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;

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

 

 

#include <iostream>
#include  <string>
#include  <vector>
#include  <algorithm>
#include  <unordered_map>

using namespace std;
typedef pair<int,int> 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<<pos;
    unordered_map<int,int>  map;
    string s;
    vector<string> strvec;

    vector<int>  vec;
    int n;
    int res;
    cin>>s>>n;
    vector<string> t(n);
    for(int i=0;i<n;i++){
       cin>>t[i];
       strvec.push_back(t[i]);
       res=strIndex(s,strvec[i]);
       map.insert(make_pair(i,res));
    }
   vector<PAIR> 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<PAIR>::iterator it=map_vec.begin();
  cout<<it->first;

   for(int i=it->second+strvec[it->first].size();i<s.size();i++){
     cout<<s[i];
   }
   return 0;
}

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值