这道题重在数据结构的使用,要求是o(1)的时间复杂度完成上面两个操作,自然想到了哈希。 第一种解法:用数组实现哈希 #include <vector> #include <iostream> #include <string.h> using namespace std; //ector< vector<int> > A; int A[8000] = {0}; int latest; int count; void get(int key ) { if(A[key] !=0) { cout << A[key] << endl; } else cout << -1 << endl; } void put(int key ,int val) { A[key] = val; latest = key; count +=1; } int main() { int num , m , n; //string s; char s ; cin >> num ; //cin >> s; while( cin >> s) { if( s == 'p') {