Initial commit

This commit is contained in:
2022-06-01 20:14:50 +03:00
commit 8b9cce4739
1094 changed files with 68851 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
#include <iostream>
#include <fstream>
#include <unordered_map>
using namespace std;
ifstream input_stream;
ofstream output_stream;
int findAnagramPos(string content, string title) {
const uint32_t CONTENT_LEN = content.length();
const uint32_t TITLE_LEN = title.length();
unordered_map<char, uint32_t> target, anagram;
for (uint32_t i = 0; i < TITLE_LEN; i++) {
target[title[i]]++;
anagram[content[i]]++;
}
if (target == anagram) {
return 0;
}
for (uint32_t i = TITLE_LEN; i < CONTENT_LEN; i++) {
/**
Old first character pos = i - TITLE_LEN
New first character pos = i - TITLE_LEN + 1
Old last character pos = i - 1
New last character pos = i
*/
char firstChar = content[i - TITLE_LEN];
anagram[firstChar]--;
if (anagram[firstChar] == 0) {
anagram.erase(firstChar);
}
anagram[content[i]]++;
if (target == anagram) {
return i - TITLE_LEN;
}
}
return -1;
}
int main() {
input_stream.open("input.txt");
string content, title;
input_stream >> content;
input_stream >> title;
cout << findAnagramPos(content, title) << endl;
input_stream.close();
return 0;
}

View File

@@ -0,0 +1,111 @@
#include <iostream>
#include <fstream>
#include <unordered_map>
using namespace std;
ifstream input_stream;
ofstream output_stream;
struct Node {
public:
uint32_t number;
Node *left, *right;
Node(uint32_t n):number(n) {
}
Node(): number(0) {
}
};
bool locateLeftmostNode (Node *node, uint32_t level, uint32_t targetLevel) {
if (level == targetLevel) {
cout << node -> number << " ";
return 1;
} else {
// Search left. If unsuccessfull, search through right
if (node -> left != nullptr) {
if (locateLeftmostNode(node -> left, level + 1, targetLevel)) {
return 1;
}
}
// Search right. If unsuccessful, this should return 0
if (node -> right != nullptr) {
return locateLeftmostNode(node -> right, level + 1, targetLevel);
}
// Previous searches didn't return anything, meaning node has no children. Return 0
return 0;
}
}
bool locateRightmostNode (Node *node, uint32_t level, uint32_t targetLevel) {
if (level == targetLevel) {
cout << node -> number << endl;
return 1;
} else {
// Search right. If unsuccessfull, search through left
if (node -> right != nullptr) {
if (locateRightmostNode(node -> right, level + 1, targetLevel)) {
return 1;
}
}
// Search left. If unsuccessful, this should return 0
if (node -> left != nullptr) {
return locateRightmostNode(node -> left, level + 1, targetLevel);
}
// Previous searches didn't return anything, meaning node has no children. Return 0
return 0;
}
}
int main() {
input_stream.open("input.txt");
uint32_t N;
uint16_t K;
input_stream >> N;
input_stream >> K;
unordered_map<uint32_t, Node*> orphanNodes;
Node *nodes[N];
for (uint32_t i = 0; i < N; i++) {
nodes[i] = new Node(i + 1);
orphanNodes[i + 1] = nodes[i];
}
for (uint32_t i = 0; i < (N - 1); i++) {
uint32_t parentId, childId;
char direction;
input_stream >> parentId;
input_stream >> childId;
input_stream >> direction;
// Remove orphan mark from child
orphanNodes.erase(childId);
// Assign child to parent
if(direction == 'S') {
nodes[parentId - 1] -> left = nodes[childId - 1];
} else {
nodes[parentId - 1] -> right = nodes[childId - 1];
}
}
Node *rootNode = orphanNodes.begin() -> second;
locateLeftmostNode(rootNode, 0, K);
locateRightmostNode(rootNode, 0, K);
input_stream.close();
return 0;
}

View File

@@ -0,0 +1,66 @@
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <list>
using namespace std;
ifstream input_stream;
ofstream output_stream;
void fillList(list<uint16_t> &L) {
int counter = 1;
for (auto i = L.begin(); i != L.end(); i++) {
*i = counter;
counter++;
}
}
list<uint16_t> getPositions() {
uint16_t N, K, R;
input_stream >> N;
input_stream >> K;
input_stream >> R;
list<uint16_t> positions (N, 0);
fillList(positions);
for (uint16_t i; i < R; i++) {
uint16_t loser;
input_stream >> loser;
auto loser_it = positions.begin();
advance(loser_it, loser-1);
loser = *loser_it;
positions.erase(loser_it);
positions.push_back(loser);
}
return positions;
}
int main() {
input_stream.open("input.txt");
output_stream.open("output.txt");
list<uint16_t> positions = getPositions();
uint16_t P;
input_stream >> P;
auto dan_it = positions.begin();
advance(dan_it, P - 1);
cout << *dan_it << endl;
input_stream.close();
output_stream.close();
return 0;
}

View File

@@ -0,0 +1,44 @@
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
ifstream input_stream;
ofstream output_stream;
int main() {
input_stream.open("input.txt");
output_stream.open("output.txt");
uint32_t N, K, R, P;
input_stream >> N;
input_stream >> K;
input_stream >> R;
uint32_t rounds[R] = {};
for (uint32_t i = 0; i < R; i++) {
input_stream >> rounds[i];
}
input_stream >> P;
for (int i = R - 1; i >=0; i--) {
if (P == N) {
P = rounds[i];
} else if (P >= rounds[i]) {
P++;
}
}
cout << P << endl;
input_stream.close();
output_stream.close();
return 0;
}

Binary file not shown.