codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
#include <iostream> #include <locale> #include <string> #include <set> #include <functional> bool good(const std::string& s) { std::set<char> unique; for (std::string::const_iterator it = s.begin(); it != s.end(); ++it) { unique.insert(*it); if (unique.size() > 2) return false; } return true; } template <typename T> struct size_less : public std::binary_function<T, T, bool> { bool operator()(const T& v1, const T& v2) const { return v1.size() < v2.size(); } }; std::string find(const std::string& s) { if (s.size() == 1) return s; std::set<std::string, size_less<std::string> > v; for (size_t i=0; i<s.size(); i++) for (size_t j=i+1; j<s.size(); j++) { const std::string sub = s.substr(i, j-i+1); if (good(sub)) v.insert(sub); } return *v.rbegin(); } int main() { setlocale(LC_ALL, ""); const std::string s = "arrdeddsrdsreewdrdcegt"; std::cout << s << " => " << find(s) << std::endl; return 0; }
Private
[
?
]
Run code
Submit