For
の数) かどうか判断すれば OK。#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int cnt = 0;
string s;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> s;
if (s == "For") ++cnt;
}
cout << (cnt >= n / 2 + 1 ? "Yes" : "No");
return 0;
}
substr
関数などを用いて の末尾 文字だけ残せば、あとは、比較するだけ。比較は、 重ループで 、set
を使えば とか。#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
int cnt = 0;
cin >> n >> m;
vector<string> s(n);
for (int i = 0; i < n; ++i) {
cin >> s[i];
s[i] = s[i].substr(3, 3); // 末尾 3 文字だけ残す
}
string t;
set<string> ts; // T_i を set に格納
for (int i = 0; i < m; ++i) {
cin >> t;
ts.insert(t);
}
// S_i の末尾 3 文字と一致する T_i が存在するなら 1 加算
for (int i = 0; i < n; ++i) {
if (ts.find(s[i]) != ts.end()) ++cnt;
}
cout << cnt;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
void dfs(int v, int p, vector<vector<int>>& g, int& cnt) {
++cnt;
for (int a : g[v]) {
if (a != p) {
dfs(a, v, g, cnt);
}
}
return;
}
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> g(n);
for (int i = 0, u, v; i < m; ++i) {
cin >> u >> v;
--u; --v;
g[u].push_back(v); g[v].push_back(u);
}
if (m != n - 1) {
cout << "No";
return 0;
}
for (int i = 0; i < n; ++i) {
if (g[i].size() > 2) {
cout << "No";
return 0;
}
}
int cnt = 0; // 頂点 0 と連結である頂点の数 (頂点 0 を含む)
dfs(0, -1, g, cnt);
if (cnt != n) {
cout << "No";
return 0;
}
cout << "Yes";
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main() {
string s, t;
cin >> s >> t;
int sSize = s.size(), tSize = t.size();
int front, back;
for (front = 0; front < tSize; ++front) {
if (!(s[front] == '?' || t[front] == '?' || s[front] == t[front])) {
break;
}
}
for (back = 0; back < tSize; ++back) {
int sIdx = sSize - 1 - back, tIdx = tSize - 1 - back;
if (!(s[sIdx] == '?' || t[tIdx] == '?' || s[sIdx] == t[tIdx])) {
break;
}
}
for (int i = 0; i <= tSize; ++i) {
cout << (i <= front && tSize - i <= back ? "Yes" : "No") << '\n';
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
// LCP の計算
int culLCP(string s, string t) {
int sz = min(s.size(), t.size());
int ret;
for (ret = 0; ret < sz; ++ret)
if (s[ret] != t[ret]) break;
return ret;
}
int main() {
int n;
cin >> n;
// 組 (S_i, i)
vector<pair<string, int>> s(n);
for (int i = 0; i < n; ++i) cin >> s[i].first, s[i].second = i;
sort(s.begin(), s.end());
vector<int> ans(n);
for (int i = 1; i < n; ++i) {
int LCP = culLCP(s[i - 1].first, s[i].first);
ans[s[i - 1].second] = max(ans[s[i - 1].second], LCP);
ans[s[i].second] = max(ans[s[i].second], LCP);
}
for (int i = 0; i < n; ++i) cout << ans[i] << '\n';
return 0;
}
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using mint = modint998244353;
int dfs(int v, int p, vector<vector<int>>& g, vector<vector<vector<mint>>>& dp) {
if (g[v].size() == 1 && p != -1) {
return 1;
}
int ret = 1;
for (int a : g[v]) {
if (a != p) {
ret += dfs(a, v, g, dp);
int dp0Size = dp[0][a].size(), dp1Size = dp[1][a].size();
vector<mint> dp0(dp0Size), dp1(dp1Size);
for (int i = 0; i < dp0Size; ++i) dp0[i] = dp[0][a][i] + dp[1][a][i];
dp[0][v] = convolution(dp[0][v], dp0);
dp[0][v].resize(ret + 2);
for (int i = 0; i < dp1Size - 1; ++i) dp1[i] = dp[0][a][i] + dp[1][a][i + 1];
dp[1][v] = convolution(dp[1][v], dp1);
dp[1][v].resize(ret + 2);
}
}
return ret;
}
int main() {
int n;
cin >> n;
vector<vector<vector<mint>>> dp(2, vector<vector<mint>>(n, vector<mint>(2)));
vector<vector<int>> g(n);
for (int i = 0, u, v; i < n - 1; ++i) {
cin >> u >> v;
--u; --v;
g[u].push_back(v); g[v].push_back(u);
}
for (int i = 0; i < n; ++i) {
dp[0][i][0] = 1; dp[1][i][1] = 1;
}
dfs(0, -1, g, dp);
for (int i = 1; i <= n; ++i) cout << (dp[0][0][i] + dp[1][0][i]).val() << '\n';
return 0;
}
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <typename T> // 0-indexed
class BIT {
int n;
vector<T> data;
public:
BIT(int _n) : n(_n), data(n + 1, 0) {}
void add(int a, T w) {
for (int i = ++a; i <= n; i += i & -i) data[i] += w;
}
T get(int a) { return sum(a, a + 1); }
void set(int a, T w) { add(a, w - get(a)); }
T sum(int a) { // [0, a)
T ret = 0;
for (int i = a; i > 0; i -= i & -i) ret += data[i];
return ret;
}
T sum(int l, int r) { // [l, r)
T ret = 0;
for (int i = r; i > 0; i -= i & -i) ret += data[i];
for (int i = l; i > 0; i -= i & -i) ret -= data[i];
return ret;
}
};
int main() {
int n, q;
cin >> n;
vector<pair<ll, int>> card(n);
for (int i = 0, a, b; i < n; ++i) {
cin >> a >> b;
card[i] = {a, b};
}
cin >> q;
vector<tuple<int, int, ll>> query(q);
for (int i = 0, t, x, y; i < q; ++i) {
cin >> t;
if (t == 1) {
cin >> x >> y;
query[i] = {t, --x, y};
card.emplace_back(y, -1);
} else if (t == 2) {
cin >> x >> y;
query[i] = {t, --x, y};
} else if (t == 3) {
cin >> x;
query[i] = {t, x, -1};
}
}
int cardSize = card.size();
vector<int> rev(cardSize); // 得点が i 番目に大きいカードの添え字
iota(rev.begin(), rev.end(), 0);
auto f = [&](int i, int j) { return card[i].first < card[j].first; };
sort(rev.rbegin(), rev.rend(), f);
vector<int> idx(cardSize); // カード i の得点が何番目に大きいか
for (int i = 0, sz = cardSize; i < sz; ++i) {
idx[rev[i]] = i;
}
// num は カード枚数、score は (得点) * (カード枚数)
BIT<ll> num(cardSize), score(cardSize);
for (int i = 0; i < n; ++i) {
auto [a, b] = card[i];
num.add(idx[i], b);
score.add(idx[i], a * b);
}
for (int i = 0, qi = n; i < q; ++i) {
auto [t, x, y] = query[i];
if (t == 1) {
card[x].first = y; // カードの得点を書き換え
// カードの得点が変わるので、添え字も書き換え
num.set(idx[x], 0);
score.set(idx[x], 0);
idx[x] = idx[qi++];
num.set(idx[x], card[x].second);
score.set(idx[x], y * card[x].second);
} else if (t == 2) {
card[x].second = y; // カードの枚数を書き換え
num.set(idx[x], y);
score.set(idx[x], card[x].first * y);
} else if (t == 3) {
int ok = -1, ng = cardSize + 1;
for (int mid = (ok + ng) / 2; abs(ok - ng) > 1; mid = (ok + ng) / 2) {
if (num.sum(mid) < x) ok = mid;
else ng = mid;
}
if (ok == cardSize) cout << -1 << '\n';
else cout << score.sum(ok) + card[rev[ok]].first * (x - num.sum(ok)) << '\n';
}
}
return 0;
}
-1
である。#include <bits/stdc++.h>
using namespace std;
class WarshallFloyd {
private:
static const int bitSize = 2000;
int n;
vector<bitset<bitSize>> d;
vector<pair<int, int>> query;
vector<int> ans;
public:
WarshallFloyd(int v) : n(v), d(v, bitset<bitSize>()) {
for (int i = 0; i < n; i++) d[i][i] = 0;
}
void edge(int from, int to, bool cost) { d[from][to] = cost; }
vector<int> solve() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (d[j][i]) d[j] |= d[i];
}
for (int j = 0, sz = query.size(); j < sz; ++j) {
auto [u, v] = query[j];
if (d[u][v] && ans[j] == -1) ans[j] = i;
}
}
return ans;
}
// クエリ読み込み
void setQuery(vector<pair<int, int>> &q) {
query = q;
ans.assign(query.size(), -1);
}
};
int main() {
int n, m, q;
cin >> n >> m;
WarshallFloyd w(n);
for (int i = 0, u, v; i < m; ++i) {
cin >> u >> v;
--u; --v;
w.edge(u, v, true);
}
cin >> q;
vector<pair<int, int>> query(q);
for (int i = 0, u, v; i < q; ++i) {
cin >> u >> v;
--u; --v;
query[i] = {u, v};
}
w.setQuery(query);
vector<int> ans(w.solve());
for (int i = 0, sz = ans.size(); i < sz; ++i) {
auto [u, v] = query[i];
cout << (ans[i] == -1 ? -1 : max({u, v, ans[i]}) + 1) << '\n';
}
return 0;
}