Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <string>
- using namespace std;
- struct list{
- string element;
- string key;
- list* next;
- };
- struct map{
- list* a[500009];
- map(){
- for (int i = 0; i < 500009; i++)
- a[i] = nullptr;
- }
- void Put(string strKey, string el, int hashKey){
- list *findEl = a[hashKey];
- list *prevEl = findEl;
- while (findEl != nullptr && findEl->key != strKey) {
- prevEl = findEl;
- findEl = findEl->next;
- }
- if (findEl == nullptr) {
- list *newElement = new list;
- newElement->next = nullptr;
- newElement->element = el;
- newElement->key = strKey;
- if (prevEl == nullptr) {
- a[hashKey] = newElement;
- }
- else {
- prevEl->next = newElement;
- }
- }
- else {
- findEl->element = el;
- }
- }
- string Get(string strKey, int hashKey) {
- list *findEl = a[hashKey];
- while (findEl != nullptr && findEl->key != strKey) {
- findEl = findEl->next;
- }
- if (findEl == nullptr)
- return "none";
- else
- return findEl->element;
- }
- void Delete(string strKey, int hashKey){
- list* findEl = a[hashKey];
- list* prevEl = a[hashKey];
- int flag = 1;
- while(findEl != nullptr && findEl -> key != strKey){
- prevEl = findEl;
- findEl = findEl -> next;
- }
- if (findEl != nullptr && findEl -> key == strKey){
- if (findEl == a[hashKey])
- a[hashKey] = findEl -> next;
- else
- prevEl -> next = findEl -> next;
- delete(findEl);
- }
- }
- };
- int primeNum[26] = {25013, 25031, 25033, 25037, 25057, 25073,
- 25087, 25097, 25111, 25117, 25121, 25127,
- 25147, 25153, 25163, 25169, 25171, 25183,
- 25189, 25219, 25229, 25237, 25243, 25247, 25253, 25261};
- int FindHashKey(string str){
- int ans = 0, degree = 1;
- for (int i = 0; i < str.size(); i++) {
- ans = (abs(ans + (int) str[i] * degree) % 500009);
- degree *= primeNum[(int)str[0] - (int)'a'];
- }
- return ans;
- }
- int main(){
- ios_base::sync_with_stdio(false);
- ifstream in;
- ofstream out;
- in.open("map.in");
- out.open("map.out");
- in.tie(NULL);
- map Map;
- while(!in.eof()){
- string com, element, key;
- in >> com;
- if (com.size() == 0)
- break;
- if (com == "put"){
- in >> key >> element;
- Map.Put(key, element, FindHashKey(key));
- }
- if (com == "get"){
- in >> key;
- out << Map.Get(key, FindHashKey(key)) << "\n";
- }
- if (com == "delete"){
- in >> key;
- Map.Delete(key, FindHashKey(key));
- }
- }
- in.close();
- out.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement