You are viewing a single comment's thread. Return to all comments →
interesting, I did something weird and longer but fun :)
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> #include <cassert> using namespace std; template< typename... Args > std::string string_sprintf( const char* format, Args... args ) { int length = std::snprintf( nullptr, 0, format, args... ); assert( length >= 0 ); char* buf = new char[length + 1]; std::snprintf( buf, length + 1, format, args... ); std::string str( buf ); delete[] buf; return std::move(str); } void replaceAll( string &s, const string &search, const string &replace ) { for( size_t pos = 0; ; pos += replace.length() ) { // Locate the substring to replace pos = s.find( search, pos ); if( pos == string::npos ) break; // Replace by erasing and inserting s.erase( pos, search.length() ); s.insert( pos, replace ); } } int main() { string S, letters; cin>>S; for(int i=0; i<S.length(); i++) { if(count(letters.begin(),letters.end(),S[i])==0) letters+=S[i]; } //cout << "-"<<letters<<"-"<<endl; bool keepLooking = true; string w; while(keepLooking) { bool change = false; for(int i=0; i<letters.length(); i++){ w = string_sprintf("%c%c",letters[i] , letters[i]); //cout << "--" << w << "--" <<endl; if(S.find(w)!=string::npos) { replaceAll(S, w, ""); change = true; } } if(!change) keepLooking = false; } if(S.empty()) cout << "Empty String"; else cout << S; return 0; }
comments please?
Seems like cookies are disabled on this browser, please enable them to open this website
Super Reduced String
You are viewing a single comment's thread. Return to all comments →
interesting, I did something weird and longer but fun :)
comments please?