Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <string>
- using namespace std;
- class Node {
- public:
- Node* next;
- string from;
- string to;
- int amount;
- };
- class list {
- public:
- int len;
- Node* head;
- list();
- void add(string from, string to, int amount);
- Node* find(string from, string to);
- };
- list::list() {
- head = NULL;
- len = 0;
- }
- void list::add(string from, string to, int amount) {
- Node* same = find(from, to);
- if (same == NULL) {
- Node* node = new Node();
- node->from = from;
- node->to = to;
- node->amount = amount;
- node->next = this->head;
- this->head = node;
- }
- else
- same->amount += amount;
- }
- Node* list::find(string from, string to) {
- if (head == NULL) return NULL;
- Node* node = head;
- while (node->from != from || node->to != to) {
- node = node->next;
- if (node == NULL)
- break;
- }
- return node;
- }
- int hashh(string name) {
- int hashed = 1;
- for (int i = 0; i < name.length(); i+=2) {
- if (i + 1 < name.length()) {
- hashed += ((int)name[i] - (int)('_')) * ((int)name[i + 1] - (int)('_'));
- }
- }
- return hashed;
- }
- int main()
- {
- char chr;
- string from, to;
- int amount, ind;
- list* tab[15000];
- for (int i = 0; i < 15000; i++) {
- tab[i] = new list();
- }
- while (cin >> chr) {
- if (chr == '+') {
- cin >> from >> to >> amount;
- ind = hashh(from);
- tab[ind]->add(from, to, amount);
- }
- else if (chr == '?') {
- cin >> from >> to;
- ind = hashh(from);
- Node* found = tab[ind]->find(from, to);
- if (found == NULL) cout << "NULL poszedł" << endl;
- else cout << found->amount << endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement