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/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; +}