题目的意思:
给定一个数组中的几个数,一开始是不重复的,然后对每个位置的数据进行调整,问经过 几次调整后可以找到两个重复的数。
#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;
}