Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <vector>
- #include <algorithm>
- #include <windows.h>
- using namespace std;
- HANDLE hStdOut;
- int bin(wstring text, int left, int right, wchar_t c);
- int main()
- {
- hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
- wcin.imbue(locale("rus_rus.866"));
- wcout.imbue(locale("rus_rus.866"));
- wstring text;
- wcout << L"Введите строку:" << endl;
- getline(wcin, text);
- int count = 0;
- for (int i = 0; i < text.length(); i++)
- {
- wcout << text[i];
- if (text[i] == L',')
- {
- int sizeWhitespace = 0;
- int j = i + 1;
- for (; text[j] == L' '; j++, sizeWhitespace++);
- int size = 0;
- for (; j < text.length() && text[j] != L',' && text[j] != L' '; j++, size++, sizeWhitespace++);
- for (; text[j] == L' '; j++, sizeWhitespace++);
- if (text[j] != L',')
- size = 0;
- if (size >= 3)
- {
- count++;
- SetConsoleTextAttribute(hStdOut, FOREGROUND_GREEN);
- wcout << text.substr(i + 1, sizeWhitespace);
- SetConsoleTextAttribute(hStdOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
- i = j - 1;
- }
- }
- }
- wcout << L"\nНайдено " << count << endl;
- wcout << L"\nВведите символ для бинарного поиска: ";
- wchar_t c;
- wcin >> c;
- sort(begin(text), end(text));
- wcout << L"Найдено " << bin(text, 0, text.length() - 1, c) << endl;
- system("pause");
- }
- int bin(wstring text, int left, int right, wchar_t c)
- {
- if (left > right)
- return 0;
- int ctr = left + (right - left) / 2;
- if (c < text[ctr])
- return bin(text, left, ctr - 1, c);
- else if (c > text[ctr])
- return bin(text, ctr + 1, right, c);
- else
- {
- int count = 1;
- for (int tmp = ctr + 1; tmp <= right && text[tmp] == c; count++, tmp++);
- for (int tmp = ctr - 1; tmp >= left && text[tmp] == c; count++, tmp--);
- return count;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement