Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //.h
- //
- // Created by max on 26.05.18.
- //
- #ifndef LAB10_2_STRING_H
- #define LAB10_2_STRING_H
- #include <cstddef>
- #include <string>
- #include <vector>
- #include <iostream>
- class SubstringIterator;
- class String {
- private:
- std::string string, sub;
- std::vector<std::string> palindromes;
- friend class SubstringIterator;
- void palindrome_search();
- bool check_palindrome(std::string substring);
- void next_sub();
- public:
- size_t index;
- String(std::string s): string(s), index(0) {
- palindrome_search();
- }
- SubstringIterator begin();
- SubstringIterator end();
- friend std::ostream& operator<< (std::ostream& os, const String& s) {
- os << s.sub;
- return os;
- }
- };
- class SubstringIterator {
- private:
- size_t index_now, index_end;
- bool is_default;
- bool is_end() const { return index_end == index_now; }
- public:
- String *sub;
- SubstringIterator(): is_default(true) {}
- explicit SubstringIterator(String &s): sub(&s),
- index_now(s.index),
- index_end(s.palindromes.size()),
- is_default(false) {
- //std::cout << s.index << std::endl;
- }
- bool operator == (const SubstringIterator &other) const;
- bool operator != (const SubstringIterator &other) const;
- String operator* ();
- SubstringIterator& operator++ ();
- SubstringIterator operator++ (int);
- };
- #endif //LAB10_2_STRING_H
- /////////////////////////////////////////////////
- //.cpp
- //
- // Created by max on 26.05.18.
- //
- #include "String.h"
- #include <sstream>
- #include <unordered_set>
- #include <iostream>
- bool String::check_palindrome(std::string substring) {
- size_t len = substring.length();
- for(size_t i = 0; i < len / 2; i++)
- if (substring.at(i) != substring.at(len-i-1))
- return false;
- return true;
- }
- void String::palindrome_search() {
- size_t size = string.size();
- std::unordered_set<std::string> hash_set;
- std::vector<int> options;
- for (size_t i = 0; i < size; i++)
- options.push_back(0);
- int max = 1 << size;
- for (int i = 1; i < max; i++) {
- for (int j = 0; j < size; j++) {
- if (i & (1 << j))
- options.at(size_t(j)) = 1;
- else
- options.at(size_t(j)) = 0;
- }
- std::stringstream str;
- for (size_t j = 0; j < size; j++) {
- if (options[j]) {
- str << string.at(j);
- }
- }
- std::string substring;
- str >> substring;
- if (check_palindrome(substring) && (hash_set.find(substring) == hash_set.end())) {
- hash_set.emplace(substring);
- if (i == 1)
- sub = substring;
- else
- palindromes.push_back(substring);
- }
- }
- }
- void String::next_sub() {
- //std::cout << "ind:" << index << " ";
- sub = palindromes.at(index++);
- //std::cout << "sub:" << sub << " ";
- }
- SubstringIterator String::begin() {
- return SubstringIterator(*this);
- }
- SubstringIterator String::end() {
- String x = *this;
- for (; x.index != x.palindromes.size(); x.next_sub());
- return SubstringIterator(x);
- }
- bool SubstringIterator::operator==(const SubstringIterator &other) const {
- return (is_default && other.is_default) ||
- (is_default && other.is_end()) ||
- (is_end() && other.is_default) ||
- (sub->index == other.sub->index);
- }
- bool SubstringIterator::operator!=(const SubstringIterator &other) const {
- return !(*this == other);
- }
- String SubstringIterator::operator*() {
- if (is_default || is_end()) {
- throw "Not initialized iterator";
- }
- return String(*sub);
- }
- SubstringIterator& SubstringIterator::operator++() {
- if (is_default) {
- throw "Not Initialized iterator";
- }
- if (is_end()) {
- throw "Iterator overflow";
- }
- index_now++;
- sub->next_sub();
- return *this;
- }
- SubstringIterator SubstringIterator::operator++(int) {
- SubstringIterator tmp(*this);
- operator++();
- return tmp;
- }
- /////////////////////////////////////////////////////////
- //main
- #include <iostream>
- //#include "subsequence.h"
- #include "String.h"
- #include "subsequence.h"
- int main() {
- String s("qwewty");
- auto end = s.end();
- std::cout << end.sub->index << std::endl;
- try {
- for (auto it = s.begin(); it != s.end(); it++) {
- std::cout << it.sub->index << " " << end.sub->index << " ";
- //std::cout << *it << " ";
- }
- }
- catch (const char *err) {
- std::cout << err << std::endl;
- }
- std::cout << std::endl;
- std::cout << end.sub->index << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement