Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- class HashTable
- {
- public:
- size_t table_size = 2 * 256;
- public:
- HashTable(int sz) // создает и инициализирует таблицу.
- {
- hash_table.resize(sz);
- table_size = sz;
- for(size_t i = 0; i < table_size; ++i) {
- hash_table[i].second = 0;
- }
- }
- size_t hash(string a) { // хэширует строку
- if (a.size() == 1) {
- return int(a[0]);
- }
- return int(a[0] + a[1]);
- }
- bool add(string a) { // добавляет строку в хэш таблицу. Возвращает 1 если получилось добавить, 0 если нет.
- int pos = hash(a);
- while(pos < table_size && hash_table[pos].second) pos++;
- if (pos == table_size)
- return 0;
- hash_table[pos].first = a;
- hash_table[pos].second = 1;
- return 1;
- }
- pair <string, bool> get(size_t pos) {
- return hash_table[pos];
- }
- private:
- vector <pair<string, bool>> hash_table;
- };
- int main()
- {
- int cnt = 0;
- vector <string> strings;
- string s;
- freopen("/users/staff/staroverov/lectures/images/t0.txt", "r", stdin);
- // считываем строки и заносим в массив
- while (cin >> s) {
- cnt++;
- strings.push_back(s);
- }
- // создаем хэш-таблицу и добавляем туда строки
- HashTable ht(cnt * 2);
- cout << ht.table_size;
- for (size_t i = 0; i < cnt; ++i) {
- ht.add(strings[i]);
- }
- // выводим в файлы строки
- for (size_t i = 0; i < cnt * 2; ++i) {
- pair <string, bool> tmp = ht.get(i);
- if (!tmp.second) continue ;
- ofstream out;
- string num = "";
- int c = i;
- if (!c) {
- num += '0';
- } else {
- while (c) {
- num += '0' + (c % 10);
- c /= 10;
- }
- }
- reverse(num.begin(), num.end());
- num += ".txt";
- out.open(num);
- if (out.is_open()) {
- out << tmp.first << endl;
- out.close();
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement