Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions fnhid/Graph/17352.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <iostream>
#include <vector>
#include <stack>

using namespace std;
using Graph = vector<vector<int> >;

void dfs(Graph &graph, vector<bool> &visited) {
stack<int> 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<bool> 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;
}
91 changes: 91 additions & 0 deletions fnhid/Queue/3190.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <bits/stdc++.h>
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<pair<int, char> > &cmd, vector<vector<int> > &board) {
int time = 0, length = 1, dir_idx = 0, cmd_idx = 0;


pair<int, int> head = {0, 0}, tail;
queue<pair<int, int> > 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<vector<int> > board(n, vector<int>(n, 0)); // 1: apple, 2: body
board[0][0] = BODY;

pair<int, int> 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<pair<int, char> > 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;
}

55 changes: 55 additions & 0 deletions fnhid/String/1786.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <vector>
#include <iostream>

using namespace std;

vector<int> getPi(string &pattern) {
int m = pattern.size();
vector<int> 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<int> kmp(string &pattern, string &text) {
vector<int> res;
vector<int> 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<int> res = kmp(P, T);
cout << res.size() << "\n";
for (int i: res) {
cout << i + 1 << " ";
}


return 0;
}