diff --git a/fnhid/Graph/17352.cpp b/fnhid/Graph/17352.cpp new file mode 100644 index 0000000..88ec616 --- /dev/null +++ b/fnhid/Graph/17352.cpp @@ -0,0 +1,48 @@ +#include +#include +#include + +using namespace std; +using Graph = vector >; + +void dfs(Graph &graph, vector &visited) { + stack s; + s.push(1); + visited[1] = true; + while (!s.empty()) { + int v = s.top(); + s.pop(); + for (int neigh: graph[v]) { + if (!visited[neigh]) { + visited[neigh] = true; + s.push(neigh); + } + } + } +} + +int main() { + int n; + cin >> n; + Graph G(n + 1); + vector visited(n + 1, false); + for (int i = 0; i < n - 2; i++) { + int a, b; + cin >> a >> b; + G[a].push_back(b); + G[b].push_back(a); + } // set graph + + dfs(G, visited); + + int iso = -1; + for (int i = 2; i < n + 1; i++) { + if (!visited[i]) { + iso = i; + break; + } + } + + cout << 1 << " " << iso; + return 0; +} diff --git a/fnhid/Queue/3190.cpp b/fnhid/Queue/3190.cpp new file mode 100644 index 0000000..20b2d86 --- /dev/null +++ b/fnhid/Queue/3190.cpp @@ -0,0 +1,91 @@ +#include +using namespace std; + +enum WORDS { + VOID, + APPLE, + BODY +}; + +#define X first +#define Y second + +int dx[4] = {0, 1, 0, -1}; +int dy[4] = {1, 0, -1, 0}; + +int solve(int n, vector > &cmd, vector > &board) { + int time = 0, length = 1, dir_idx = 0, cmd_idx = 0; + + + pair head = {0, 0}, tail; + queue > q; + + q.push({0, 0}); + board[0][0] = BODY; + + while (true) { + time++; + //move + head.X += dx[dir_idx]; + head.Y += dy[dir_idx]; + + // crash check + if (head.X < 0 || head.Y < 0 || head.X >= n || head.Y >= n || + board[head.X][head.Y] == BODY) + break; + + if (board[head.X][head.Y] != APPLE) { + tail = q.front(); + q.pop(); + board[tail.first][tail.second] = VOID; + } + q.push(head); + board[head.X][head.Y] = BODY; + + + // dir change for next time + if (cmd_idx < cmd.size() && cmd[cmd_idx].first == time) { + if (cmd[cmd_idx].second == 'D') { + dir_idx = (dir_idx + 1) % 4; + } else { + // 'L' + dir_idx = (dir_idx + 3) % 4; // 3 mod 4 == -1 mod 4 + } + cmd_idx++; + } + } + return time; +} + + +int main() { + cin.tie(nullptr)->sync_with_stdio(false); + int n, k; + cin >> n >> k; + + vector > board(n, vector(n, 0)); // 1: apple, 2: body + board[0][0] = BODY; + + pair apl; // first: row, second: column + + + for (int i = 0; i < k; i++) { + cin >> apl.first >> apl.second; + + apl.first--; + apl.second--; + board[apl.first][apl.second] = APPLE; + } + + int l; + cin >> l; + + vector > cmd(l, {0, 0}); // first: time, second: dir idx + for (int i = 0; i < l; i++) { + cin >> cmd[i].first >> cmd[i].second; + } + + cout << solve(n, cmd, board); + return 0; +} + diff --git a/fnhid/String/1786.cpp b/fnhid/String/1786.cpp new file mode 100644 index 0000000..462de0f --- /dev/null +++ b/fnhid/String/1786.cpp @@ -0,0 +1,55 @@ +#include +#include + +using namespace std; + +vector getPi(string &pattern) { + int m = pattern.size(); + vector pi(m, 0); + int j = 0; + + + for (int i = 1; i < m; ++i) { + while (j > 0 && pattern[i] != pattern[j]) j = pi[j - 1]; + if (pattern[i] == pattern[j]) { + j++; + pi[i] = j; + } + } + return pi; +} + +vector kmp(string &pattern, string &text) { + vector res; + vector pi = getPi(pattern); + int j = 0; + int n = text.size(), m = pattern.size(); + for (int i = 0; i < n; ++i) { + while (j > 0 && text[i] != pattern[j]) j = pi[j - 1]; + if (text[i] == pattern[j]) { + if (j == m - 1) { + // success + res.push_back(i - m + 1); + j = pi[j]; + } else { + j++; + } + } + } + return res; +} + +int main() { + cin.tie(nullptr)->sync_with_stdio(false); + string P, T; + getline(cin, T); + getline(cin, P); + vector res = kmp(P, T); + cout << res.size() << "\n"; + for (int i: res) { + cout << i + 1 << " "; + } + + + return 0; +}