Skip to content

Instantly share code, notes, and snippets.

@0xHexE
Last active April 4, 2019 02:22
Show Gist options
  • Save 0xHexE/63b6d21bd378e00a795738d98e49ab33 to your computer and use it in GitHub Desktop.
Save 0xHexE/63b6d21bd378e00a795738d98e49ab33 to your computer and use it in GitHub Desktop.
C++ Code for the River finding
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
int number_of_words;
cout << "Enter the number of words: ";
cin >> number_of_words;
vector<int> vector_instance;
if (number_of_words <= 0) {
cout << "Invalid number of worlds";
return 0;
}
int max_string_length = 0;
for (int i = 0; i < number_of_words; i++) {
cout << "Enter the word ";
string basic_string;
cin >> basic_string;
vector_instance.push_back(static_cast<int &&>(basic_string.size()));
max_string_length = max(max_string_length, vector_instance.back());
}
int ret = 0, retw = -1;
for (auto string_length = static_cast<unsigned long>(max_string_length);; string_length++) {
vector<int> current_item(string_length), next_item(string_length);
int cs = -1, new_line = 1;
for (int current_item_of_vector : vector_instance) {
if (cs + current_item_of_vector >= string_length) {
current_item.swap(next_item);
next_item = vector<int>(string_length);
cs = -1;
new_line++;
}
if (cs >= 0) {
next_item[cs] = max(current_item[cs - 1] + 1, max(current_item[cs] + 1, current_item[cs + 1] + 1));
if (next_item[cs] > ret) {
ret = next_item[cs];
retw = static_cast<int>(string_length);
}
}
cs += current_item_of_vector + 1;
}
if (ret >= new_line) {
break;
}
}
cout << "Rever width " << retw << " Revier " << ret << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment