Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <string>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <windows.h>
  9.  
  10. using namespace std;
  11.  
  12. HANDLE hStdOut;
  13.  
  14. int bin(wstring text, int left, int right, wchar_t c);
  15.  
  16. int main()
  17. {
  18. hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  19. wcin.imbue(locale("rus_rus.866"));
  20. wcout.imbue(locale("rus_rus.866"));
  21. wstring text;
  22. wcout << L"Введите строку:" << endl;
  23. getline(wcin, text);
  24. int count = 0;
  25. for (int i = 0; i < text.length(); i++)
  26. {
  27. wcout << text[i];
  28. if (text[i] == L',')
  29. {
  30. int sizeWhitespace = 0;
  31. int j = i + 1;
  32. for (; text[j] == L' '; j++, sizeWhitespace++);
  33. int size = 0;
  34. for (; j < text.length() && text[j] != L',' && text[j] != L' '; j++, size++, sizeWhitespace++);
  35. for (; text[j] == L' '; j++, sizeWhitespace++);
  36. if (text[j] != L',')
  37. size = 0;
  38. if (size >= 3)
  39. {
  40. count++;
  41. SetConsoleTextAttribute(hStdOut, FOREGROUND_GREEN);
  42. wcout << text.substr(i + 1, sizeWhitespace);
  43. SetConsoleTextAttribute(hStdOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
  44. i = j - 1;
  45. }
  46. }
  47. }
  48. wcout << L"\nНайдено " << count << endl;
  49. wcout << L"\nВведите символ для бинарного поиска: ";
  50. wchar_t c;
  51. wcin >> c;
  52. sort(begin(text), end(text));
  53. wcout << L"Найдено " << bin(text, 0, text.length() - 1, c) << endl;
  54. system("pause");
  55. }
  56.  
  57. int bin(wstring text, int left, int right, wchar_t c)
  58. {
  59. if (left > right)
  60. return 0;
  61. int ctr = left + (right - left) / 2;
  62. if (c < text[ctr])
  63. return bin(text, left, ctr - 1, c);
  64. else if (c > text[ctr])
  65. return bin(text, ctr + 1, right, c);
  66. else
  67. {
  68. int count = 1;
  69. for (int tmp = ctr + 1; tmp <= right && text[tmp] == c; count++, tmp++);
  70. for (int tmp = ctr - 1; tmp >= left && text[tmp] == c; count++, tmp--);
  71. return count;
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement