Skip to content

Instantly share code, notes, and snippets.

@JVero
Last active December 10, 2018 02:58
Show Gist options
  • Save JVero/ec41fd9abe5ed5c55e9c36fc76c0d176 to your computer and use it in GitHub Desktop.
Save JVero/ec41fd9abe5ed5c55e9c36fc76c0d176 to your computer and use it in GitHub Desktop.
#include <string>
#include <iostream>
#include<utility>
#include<algorithm>
#include<fstream>
#include<thread>
#include<functional>
#include<array>
#include<vector>
std::string removeCharFromString(std::string input, char target){
std::string result;
for (auto &c: input) {
if ((c != target) && (c+32 != target) && (c-32 && target)){
result.push_back(c);
}
}
return result;
}
std::string decomposeWithStack(std::string input) {
std::vector<char> returnVal;
returnVal.push_back(0); // otherwise the .back call segfaults;
for (auto &c: input) {
if (c+32 == returnVal.back() || c-32 == returnVal.back()) {
returnVal.pop_back();
} else {
returnVal.push_back(c);
}
}
return std::string(returnVal.begin()+1, returnVal.end()); // to compensate for that first push_back
}
// Pretty much useless at this point, overtaken by the function above
std::string decomposeString(std::string input) {
if (input.length() < 2){ return input; }
for (auto i = 0; i != input.length()-1; i++) {
if (input[i]+32==input[i+1] || input[i]-32==input[i+1]) {
return std::move(input.erase(i, 2));
}
}
return input;
}
std::string totalDecomposition(std::string input) {
std::string result;
while (true) {
//result = decomposeString(input);
result = decomposeWithStack(input);
if (result == input) { // decompose didnt change anything
return result;
}
input = result;
}
}
void totalOperation(std::string input, const char target, std::array<std::string, 26> &stringplace) {
auto trunc = removeCharFromString(input, target);
stringplace[(int)target-97] = totalDecomposition(trunc);
}
int main() {
std::ifstream t{"input.txt"};
std::string ex;
std::getline(t, ex);
auto result = totalDecomposition(ex);
std::cout << "Actually removed" << (ex.size() - result.size()) << std::endl;
int removed;
std::string shortest_string(1<<20, 'a');
char most_impactful_letter;
std::thread threads[26];
std::array<std::string, 26> truncatedStrings;
for (char i = 0; i != 26; i++) {
threads[i] = std::thread{totalOperation, result, (char)i+97, std::ref(truncatedStrings)};
}
for (char i = 0; i != 26; i++) {
threads[i].join();
}
char letter = 97;
char myletter = -1;
for (auto &s: truncatedStrings) {
if (s.size() < shortest_string.size()) {
std::cout << s << " " << letter << std::endl;
shortest_string = s;
myletter = letter;
}
letter++;
}
std::cout << "Shortest string is " << "a" <<" with length " << shortest_string.size() << std::endl;
std::cout << "The letter was " << myletter << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment