Skip to content
Open
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
42 changes: 42 additions & 0 deletions unk2001/unk2001_Counting_Bits.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Word Combinations
// cses.fi/1146
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

ll n;

ll get(int sz) {
return 1ll*sz*(1ll << (sz-1));
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n;

vector<int> binary;
while (n) {
binary.push_back(n % 2);
n /= 2;
}
reverse(binary.begin(), binary.end());
ll res = 0;
int len = binary.size();
for (int i = 1; i <= binary.size(); i++) {
int x = binary[i-1];
if (x == 0) continue;
res += get(len - i);
}

ll order = 0;
for (int i = binary.size(); i >= 1; i--) {
int x = binary[i-1];
if (x == 0) continue;
res += order;
order += (1ll << (len-i));
}
for (auto x : binary) res += x;
cout <<res;
}
// author: unk2001
// profile: https://cses.fi/user/13599
49 changes: 49 additions & 0 deletions unk2001/unk2001_Shortest_Subsequence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Word Combinations
// cses.fi/1087
#include <bits/stdc++.h>
#define FOR(i, u, v) for (int i = u; i <= v; i++)
#define FORD(i, v, u) for (int i = v; i >= u; i--)
#define pii pair<int, int>
#define mp make_pair
#define F first
#define S second
#define PB push_back
#define N 1000005

using namespace std;

string s;
int n, nxt[N][4];

string DNA = "ACGT";

int main() {
ios_base::sync_with_stdio(0); cin.tie(0);

cin >> s;
n = s.size();
s = " " + s;
FOR(i, 0, 3) nxt[n+1][i] = n+1;
FORD(i, n, 1) {
FOR(j, 0, 3) nxt[i][j] = nxt[i+1][j];
int x = DNA.find(s[i]);
nxt[i][x] = i;
}

string ans = "";
int curPos = 1;
while (curPos <= n+1) {
int best = 0;
FOR(j, 1, 3) {
if (nxt[curPos][j] > nxt[curPos][best]) {
best = j;
}
}
curPos = nxt[curPos][best];
ans.push_back(DNA[best]);
curPos++;
}
cout <<ans;
}
// author: unk2001
// profile: https://cses.fi/user/13599
46 changes: 46 additions & 0 deletions unk2001/unk2001_Tree_Diameter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Word Combinations
// cses.fi/1131
#include <bits/stdc++.h>

using namespace std;
const int N = 200006;

int n;
vector<int> g[N];

pair<int, int> BFS(int pivot) {
queue<int> q;
vector<int> d(n+1, -1);
d[pivot] = 0; q.push(pivot);

while (!q.empty()) {
int u = q.front();
q.pop();
for (auto v : g[u]) {
if (d[v] == -1) {
d[v] = d[u] + 1;
q.push(v);
}
}
}
for (int i = 1; i <= n; i++) {
if (d[pivot] < d[i]) {
pivot = i;
}
}
return make_pair(pivot, d[pivot]);
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n;
for (int i = 1; i < n; i++) {
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
auto z1 = BFS(1);
auto z2 = BFS(z1.first);
cout <<z2.second;
}
// author: unk2001
// profile: https://cses.fi/user/13599
85 changes: 85 additions & 0 deletions unk2001/unk2001_Word_Combinations.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Word Combinations
// cses.fi/1731
#include <bits/stdc++.h>

using namespace std;

const int N = 5005;

struct Node {
Node *child[26];
bool isWord;
Node() {
for (int i = 0; i < 26; i++) {
child[i] = NULL;
}
isWord = false;
}
};

struct Trie {
Node *root;

Trie() {
root = new Node();
}

Node* getRoot() {
return root;
}
Node* getNext(Node *p, char c) {
if (p->child[c-'a'] == NULL) {
return NULL;
} else {
return p->child[c-'a'];
}
}

void addWord(string s) {
Node *p = root;
for (auto c : s) {
if (p->child[c-'a'] == NULL) {
p->child[c-'a'] = new Node();
}
p = p->child[c-'a'];
}
p->isWord = true;
}
}trie;

int n, m, dp[N];
string s;

const int mod = 1000000007;
void inline add(int &A, int B) {
A += B;
if (A >= mod) A -= mod;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> s;
n = s.size(); s = " " + s;
cin >> m;
for (int i = 1; i <= m; i++) {
string t; cin >> t;
reverse(t.begin(), t.end());
trie.addWord(t);
}

dp[0] = 1;
for (int i = 1; i <= n; i++) {
Node *p = trie.getRoot();
for (int j = i; j >= 1; j--) {
p = trie.getNext(p, s[j]);
if (p == NULL) {
break;
} else if (p->isWord == true) {
add(dp[i], dp[j-1]);
}
}
}
cout <<dp[n];
}

// author: unk2001
// profile: https://cses.fi/user/13599