I've create a program that finds mixed duo within a string. It removes characters from a string that do not form the first mixed duo. It does not rearrange the order of the string, it ony remove characters to reveal the mixed duo...... aka "abababab", "chchchchc", "!s!s!s!".
Tell me what do you think of this code? Is my solution too complex? is it too slow?, is the code messy? is it maintainable?, am I following industry norms? Your honest opinion and critique of my style, logic, and approach is what I desire.
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
struct Data{
std::map<char,char> data1;
std::string data2;
};
bool finalVerification(std::string input){
////////////////
//DECLARATIONS//
////////////////
int size_Of_String;
char a;
char b;
//When flip = true a fails, when flip = false b fails;
bool flip;
///////////////////////
// INITIALIZATION //
///////////////////////
size_Of_String = input.size();
a = input[0];
b = input[1];
//When flip = true a fails, when flip = false b fails;
flip = false;
//////////////////////////////
//RULE CHECKING BEFORE LOGIC//
//////////////////////////////
//Last minute condition check
if(input.size() < 4){return false;}
if(input.size() == 4){
if((input[0] == input[2])&&(input[1] == input[3])){ return true;}
else {return false;}
}
//////////////////
//IMPLEMENTATION//
//////////////////
//Go through the string array from both ends.
for(int i=0; i<size_Of_String; i++)
{
if((input[i] != a) && (flip == false))
{
return false;
}
else if((input[i] != b ) && (flip == true))
{
return false;
}
if(flip == true)
{
flip = false;
}
else if (flip == false) {
flip = true;
}
}
return true;
}
Data findUniqueChar(std::string input)
{
////////////////
//DECLARATIONS//
////////////////
int size_Of_String;
int run_Through;
std::string temp;
std::map<char, char> records;
Data output;
///////////////////////
// INITIALIZATION //
///////////////////////
size_Of_String = input.size();
run_Through = size_Of_String;
temp = "";
//////////////////
//IMPLEMENTATION//
//////////////////
//Find all the unique characters and record them in this map.
for (int i=0; i<run_Through; i++){
if(input[i] != input[0])
{
//Assume this can form a twisted pair with the first element.
if(records.find(input[i]) == records.end()){
records[input[i]] = '0';
temp += input[i];
}
}
}
output.data1 = records;
output.data2 = temp;
return output;
}
std::string quickCheckFix(std::string input)
{
///////////////
//DECLARATION//
///////////////
int size_Of_String;
std::string output;
char tmp;
///////////////////////
// INITIALIZATION //
///////////////////////
size_Of_String = input.size();
output = input;
//////////////////
//IMPLEMENTATION//
//////////////////
//Go through the string array from both ends.
for(int i=0; i<size_Of_String/2; i++)
{
//If duplicates are encountered remove all instances of that character from the string and break out of the loop
if(output[i] == output[i+1])
{
tmp = output[i];
output.erase(std::remove(output.begin(), output.end(), tmp), output.end());
break;
}
else if(output[size_Of_String - 1 - i] == output[size_Of_String - 2 - i])
{
tmp = output[size_Of_String - 1 - i];
output.erase(std::remove(output.begin(), output.end(), tmp), output.end());
break;
}
}
//Call yourself again if output has been changed. set output to equal the value of the call. Use the altered output as the argument,
if(output != input){
output = quickCheckFix(output);
}
return output;
}
std::string condition(std::string input)
{ ////////////////
//DECLARATIONS//
////////////////
int size_Of_String;
std::string output;
std::string pair_Values;
std::string saved_Failures;
//False = alpha fail, true = omega fails
Data records;
///////////////////////
// INITIALIZATION //
///////////////////////
size_Of_String = input.size();
output = input;
pair_Values += input[0];
pair_Values += input[1];
saved_Failures = "";
// bool keys were replaced with chars key for more options. 0 = false, 1 = true, X = failure!
records = findUniqueChar(input);
//////////////////
//IMPLEMENTATION//
//////////////////
for(int i=0; i<size_Of_String; i++)
{
//----------------------------------//
//--------Ender conditions----------//
//----------------------------------//
//If there are no more unique characaters that could succeed it is finally safe to remove the 1st element from the string.
if(saved_Failures.size() == records.data2.size()){
//Free yourself from the loop first
break;
}
//---------------------------------------//
//--------Logic and computation----------//
//---------------------------------------//
//If the first elements was encountered again
if(input[i] == input[0])
{
//Record all true key as failures
for(int a=0; a<records.data1.size(); a++)
{
if(records.data1.find(records.data2[a])->second == '1'){
saved_Failures += records.data2[a];
records.data1[records.data2[a]] = 'X';
}
}
//Change all the false keys to true
for(int a=0; a<records.data1.size(); a++)
{
if(records.data1.find(records.data2[a])->second == '0'){
records.data1[records.data2[a]] = '1';
pair_Values[1] = input[i];
}
}
}
else if(records.data1.find(input[i])->second == '1'){ //If the key was found again while being true flip the value to false.
records.data1[input[i]] = '0';
pair_Values[1] = input[i];
}
else if(records.data1.find(input[i])->second == '0'){ //If the key was found again while being false record it as failure.
saved_Failures += input[i];
records.data1[input[i]] = 'X'; // Will never get in the loop again
}
}
//If if failed go ahead and remove the first element instances and call your friend again.
if(saved_Failures.size() == records.data2.size()){
output.erase(std::remove(output.begin(), output.end(), input[0]), output.end());
}
//If you think it succeeded remove all other characters.
else{
//Remove the 1st element character from this string
saved_Failures.erase(std::remove(saved_Failures.begin(), saved_Failures.end(), input[0]), saved_Failures.end());
//Start from the top and work your way to the bottom.
for(int i=0; i<saved_Failures.size(); i++){
output.erase(std::remove(output.begin(), output.end(), saved_Failures[i]), output.end());
}
}
return output;
}
std::string findPair(std::string input)
{
///////////////
//DECLARATION//
///////////////
std::string output;
std::string pair_Values;
////////////////////////////////////////////////
//RULE CHECKING BEFORE LOGIC & INITIALIZATION //
////////////////////////////////////////////////
//Check if the string size is invalid
if(input.size() < 4){return "";}
//If the string is valid see if you can cut it down to size before processing. Apply rule 1 again in case you got lucky
input = quickCheckFix(input);
if(input.size() < 4){return "";}
//Super lucky condition just to save more time.
if(input.size() == 4){
if((input[0] == input[2])&&(input[1] == input[3])){ return input;}
else {return "";}
}
///////////////////////
// INITIALIZATION //
///////////////////////
//Only munipulate the output string, and begin the set up for the logic loop
output = input;
pair_Values += output[0];
pair_Values += output[1];
//////////////
// LOGIC //
//////////////
output = condition(output);
//If the function return false, it is on
switch(finalVerification(output)){
case true: break;
case false: output = findPair(output);
}
return output;
}
void autoStringRead()
{
///////////////
//DECLARATION//
///////////////
std::ifstream inputs( "pairs-in.txt" );
std::string line;
std::string done;
std::ofstream results;
///////////////////////
// INITIALIZATION //
///////////////////////
//Generate the file
results.open ("results.txt");
//Make sure this exists
if( inputs == NULL ) { return; }
while (getline(inputs, line)) {
done = findPair(line);
results << done <<std::endl;
std::cout<< line << std::endl;
std::cout<< done << std::endl;
}
inputs.close();
results.close();
}
int main()
{
autoStringRead();
system("pause");
return 0;
}