Pagini recente » Cod sursa (job #2708103) | Rating Pascu Iustin (pascuiustin123) | Rating Apostolache Luca (lucap06) | Cod sursa (job #3357781) | Cod sursa (job #3357929)
#include <iostream>
#include <fstream>
struct TrieNode {
int words = 0;
int in_words = 0;
};
TrieNode graph[100][26][26];
void dfs(const std::string &str, bool subtract = false, int row = 0, int ptr = 0) {
if (ptr == str.size()) {
if (ptr > 0) {
if (!subtract) graph[row][str[ptr - 1] - 'a'][str[ptr] - 'a'].words++;
else graph[row][str[ptr - 1] - 'a'][str[ptr] - 'a'].words--;
if (!subtract) graph[row][str[ptr - 1] - 'a'][str[ptr] - 'a'].in_words++;
else graph[row][str[ptr - 1] - 'a'][str[ptr] - 'a'].in_words--;
}
return;
}
if (ptr > 0) {
if (!subtract) graph[row][str[ptr - 1] - 'a'][str[ptr] - 'a'].in_words++;
else graph[row][str[ptr - 1] - 'a'][str[ptr] - 'a'].in_words--;
}
dfs(str, subtract, row + 1, ptr + 1);
}
int count(const std::string &str, int row = 0, int ptr = 0) {
if (ptr == str.size()) return graph[row][str[ptr - 1] - 'a'][str[ptr] - 'a'].words;
return count(str, row + 1, ptr + 1);
}
int find_max(const std::string &str, int row = 0, int ptr = 0) {
if (ptr == str.size()) return row;
if (graph[row][str[ptr - 1] - 'a'][str[ptr] - 'a'].in_words > 0) {
return find_max(str, row + 1, ptr + 1);
} else {
return row;
}
}
int main() {
std::ifstream input("trie.in");
std::ofstream output("trie.out");
int op;
std::string str;
while (input >> op >> str) {
if (op == 0) dfs(str);
else if (op == 1) dfs(str, true);
else if (op == 2) output << count(str) << '\n';
else output << find_max(str) << '\n';
}
return 0;
}