Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///*
- //#include <iostream>
- //#include <vector>
- //#include <typeinfo>
- //#include <string>
- //
- //// Предложить пользователю ввести последовательность целых чисел и найти среди них большее.
- //// Если среди последовательности было отрицательное число или вовсе не число предложить снова ввести последовательность
- //
- //int a = 0;
- //
- //struct A {
- // int f;
- // int j;
- //};
- //
- //int main() {
- // std::cout << "Enter a subsequence of positive integers, enter 'exit' to stop input:" << std::endl;
- // std::vector <int> sub;
- // std::string x;
- // unsigned max = 0;
- // while (true) {
- // std::cin >> x;
- // if (x == "exit") {
- // break;
- // }
- // int tmp = 0;
- // try {
- // tmp = std::stoi(x);
- // } catch (std::invalid_argument) {
- // std::cout << "Entered value isn't positive integer. Try again" << std::endl;
- // std::cin.clear();
- // std::cin.ignore(32767, '\n');
- // sub.resize(0);
- // max = -1;
- // }
- //
- // if (std::cin.fail() || tmp < 0) {
- // std::cin.clear();
- // std::cin.ignore(32767, '\n');
- // std::cout << "Entered value isn't positive integer. Try again " << std::endl;
- // sub.clear();
- // max = -1;
- // }
- // else {
- // std::cin.ignore(32767, '\n');
- // sub.push_back(tmp);
- // if (max < tmp) {
- // max = tmp;
- // }
- // }
- // }
- // std::cout << "\nEntered subsequance is:" << std::endl;
- // for (int i = 0; i < sub.size(); ++i) {
- // std::cout << sub[i] << " ";
- // }
- // std::cout << "\nMaximum = " << max << std::endl;
- // return 0;
- //}*/
- //
- //
- //#include <cstring>
- //#include <iostream>
- //#include <climits>
- //
- //class Deq {
- //private:
- // int beg = 65536;
- // int end = 65536;
- // int array[131072] = {};
- //public:
- // Deq() {
- // for (unsigned i = 0; i < 131072; ++i) {
- // array[i] = INT_MIN;
- // }
- // }
- // void PushBack(int n) {
- // array[++end] = n;
- // std::cout << "ok" << std::endl;
- // }
- // void PushFront(int n) {
- // array[--beg] = n;
- // std::cout << "ok" << std::endl;
- // }
- // void PopFront() {
- // if (end - beg <= 0) {
- // std::cout << "error" << std::endl;
- // } else {
- // std::cout << array[beg] << std::endl;
- // beg++;
- // }
- // }
- // void PopBack() {
- // if (end - beg <= 0) {
- // std::cout << "error" << std::endl;
- // } else {
- // std::cout << array[end] << std::endl;
- // end--;
- // }
- // }
- // void Front() {
- // if ((end - beg <= 0) || (array[beg] == INT_MIN)) {
- // std::cout << "error" << std::endl;
- // } else {
- // std::cout << array[beg] << std::endl;
- // }
- // }
- // void Back() {
- // if ((end - beg <= 0) || (array[end] == INT_MIN)) {
- // std::cout << "error" << std::endl;
- // } else {
- // std::cout << array[end] << std::endl;
- // }
- // }
- // void Size() {
- // std::cout << end - beg << std::endl;
- // }
- // void Clear() {
- // beg = 65536;
- // end = 65536;
- // for (unsigned i = 0; i < 131072; ++i) {
- // array[i] = INT_MIN;
- // }
- // std::cout << "ok" << std::endl;
- // }
- //};
- //int main() {
- // int number_of_commands = 0;
- // std::cin >> number_of_commands;
- // Deq A;
- // int n;
- // char push_f_command[] = "push_front";
- // char push_b_command[] = "push_back";
- // char pop_f_command[] = "pop_front";
- // char pop_b_command[] = "pop_back";
- // char front_command[] = "front";
- // char back_command[] = "back";
- // char size_command[] = "size";
- // char clear_command[] = "clear";
- // char exit_command[] = "exit";
- // char *command = new char[11];
- // for (int i = 0; i < number_of_commands; i++) {
- // std::cin >> command;
- // if (!strcmp(command, push_f_command)) {
- // std::cin >> n;
- // A.PushFront(n);
- // }
- // if (!strcmp(command, push_b_command)) {
- // std::cin >> n;
- // A.PushBack(n);
- // }
- // if (!strcmp(command, pop_f_command)) {
- // A.PopFront();
- // }
- // if (!strcmp(command, pop_b_command)) {
- // A.PopBack();
- // }
- // if (!strcmp(command, front_command)) {
- // A.Front();
- // }
- // if (!strcmp(command, back_command)) {
- // A.Back();
- // }
- // if (!strcmp(command, size_command)) {
- // A.Size();
- // }
- // if (!strcmp(command, clear_command)) {
- // A.Clear();
- // }
- // if (!strcmp(command, exit_command)) {
- // std::cout << "bye";
- // break;
- // }
- // for (size_t j = 0; j < 11; ++j) {
- // command[j] = '\0';
- // }
- // }
- // delete[] command;
- // return 0;
- //}
- #include <iostream>
- #include <cmath>
- #include <string>
- #include <list>
- #include <vector>
- #include <iterator>
- #include <algorithm>
- const short x = 263;
- const long long p = 1'000'000'007;
- using hash_chains = std::vector<std::list<std::string>>;
- long long HashingFunction(const std::string &str, int size_table) {
- size_t len = str.size();
- long long h = 0;
- for (int i = 0; i < len; ++i) {
- h = ((((long long)(static_cast<int> (str[i]) * pow(x, i))) % p) + p) % p;
- }
- h = ((h % size_table) + size_table) % size_table;
- std::cout << h << std::endl;
- return h;
- }
- void add(hash_chains &table, const std::string &str, const int size_table) {
- long long hash = HashingFunction(str, size_table);
- table[hash].push_front(str);
- }
- void del(hash_chains &table, const std::string &str, const int size_table) {
- long long hash = HashingFunction(str, size_table);
- auto it = std::find(table[hash].begin(), table[hash].end(), str);
- // for (auto &item) { // for (it = s.begin(), it < s.end(), ++i)
- // if ()
- // }
- it = table[hash].erase(it);
- }
- void find(hash_chains &table, const std::string &str, const int size_table) {
- long long hash = HashingFunction(str, size_table);
- auto it = std::find(table[hash].begin(), table[hash].end(), str);
- if (it != table[hash].end()) {
- std::cout << "yes" << std::endl;
- }
- else {
- std::cout << "end" << std::endl;
- }
- }
- void check(hash_chains &table, long long index) {
- for (auto &item : table[index]) {
- std::cout << item << " ";
- }
- std::cout << std::endl;
- }
- int main() {
- int size_table(0), command_amount(0);
- std::cout << "Enter size_table - a size of hash table: ";
- std::cin >> size_table;
- std::cout << "Enter command_amount - a quantity of requests: ";
- std::cin >> command_amount;
- std::vector<std::list<std::string>> table(size_table, std::list<std::string>(command_amount));
- std::cout << std::endl;
- const std::string command_add = "add";
- const std::string command_del = "del";
- const std::string command_find = "find";
- const std::string command_check = "check";
- std::string entered_command;
- std::string entered_str;
- long long entered_val = 0;
- for (int i = 0; i < command_amount; ++i) {
- std::cin >> entered_command;
- if (entered_command == command_add) {
- std::cin >> entered_str;
- add(table, entered_str, size_table);
- }
- else if (entered_command == command_del) {
- std::cin >> entered_str;
- del(table, entered_str, size_table);
- }
- else if (entered_command == command_find) {
- std::cin >> entered_str;
- find(table, entered_str, size_table);
- }
- else if (entered_command == command_check) {
- std::cin >> entered_val;
- check(table, entered_val);
- entered_val = 0;
- }
- entered_command.clear();
- entered_str.clear();
- }
- table.clear();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement