赞
踩
#include <iostream>
#include <cstring>
using namespace std;
int n;
int cnt[30];
bool check(string s, string t) {
memset(cnt, 0, sizeof cnt);
if (s.size() == 1) {
return true;
}
for(int i = 0; i < s.size(); i++) {
cnt[s[i] - 'a']++;
cnt[t[i] - 'a']--;
}
int num = 0;
for (int i = 0; i < 26; i++) {
if (abs(cnt[i]) == 1) num++;
if (num > 2) return false;
}
return true;
}
int main() {
cin >> n;
string s, t;
while(n--) {
cin >> s >> t;
if (check(s, t)) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
#include <iostream>
#include <cstring>
using namespace std;
int n;
int cnt[30];
bool check(string s, string t) {
memset(cnt, 0, sizeof cnt);
if (s.size() == 1) {
return true;
}
for(int i = 0; i < s.size(); i++) {
cnt[s[i] - 'a']++;
cnt[t[i] - 'a']--;
}
int num = 0;
for (int i = 0; i < 26; i++) {
num += abs(cnt[i]);
if (num > 2) return false;
}
return true;
}
int main() {
cin >> n;
string s, t;
while(n--) {
cin >> s >> t;
if (check(s, t)) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
#include <iostream>
using namespace std;
const int N = 1e5 + 01, M = 1e9 + 10;
int a[N], n, p[N], b[N];
int pos[M];
int main()
{
int ans = 0;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> p[i];
}
for (int i = 1; i <= n; i++) {
cin >> b[i];
}
for (int i= 1; i <= n; i++) {
pos[p[i]]++;
pos[i] += pos[i - 1];
int l = max(0, i - b[i] - 1), r = min(n, i + b[i] + 1 );
if ((pos[i - 1] - pos[l] == 0) && (pos[r] - pos[i] == 0)) {
ans++;
}
}
cout << ans;
return 0;
}
#include<iostream>
#include<algorithm>
using namespace std;
#define x first
#define y second
const int N = 1e5 + 10;
int n;
pair<int, int> pa[N];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> pa[i].x;
}
for (int i = 1; i <= n; i++) cin >> pa[i].y;
sort(pa + 1, pa + n + 1);
int ans = 0;
if (n > 1 && pa[1].x + pa[1].y < pa[2].x) ans++;
if (n > 1 && pa[n].x - pa[n].y > pa[n-1].x) ans++;
for (int i = 2; i < n; i++) {
if (pa[i].x + pa[i].y < pa[i + 1].x && pa[i].x - pa[i].y > pa[i - 1].x) ans++;
}
cout << ans;
return 0;
}
#include<iostream>
using namespace std;
const int N = 2e5 + 10;
int n, ind = 0, h[N], e[N * 2], ne[N * 2], g[N], f[N];
void add(int a, int b) {
e[++ind] = b;
ne[ind] = h[a];
h[a] = ind;
}
int ans = 0;
bool bs[N];
void dfs(int r) {
bs[r] = true;
// cout << r << " ";
for (int i = h[r]; i; i = ne[i]) {
int t = e[i];
f[r] += g[t] - 1;
if (bs[t]) continue;
dfs(t);
}
}
int main() {
cin >> n;
int x, y;
for (int i = 0; i < n - 1; i++) {
cin >> x >> y;
add(x, y);
add(y, x);
g[x]++;
g[y]++;
}
dfs(1);
for (int i = 1; i <= n; i++) {
cout << f[i] << " ";
}
return 0;
}
题意可抽象为:给你n和k,求用不小于k的任意个正整数组成和不大于n的排列(有顺序)有多少种?
分析:
看到这个问题的时候,第一个就想到的是用动态规划来做,公式推导如下。(其中,
f
[
i
]
f[i]
f[i]表示刚好达到亲密度
i
i
i有多少种方法)
f[i] = f[0 1 2 .. i - k]
f[i + 1] = f[0 1 2 3 .. i-k i+1-k] = f[i] + f[i+1-k];
f[i] = f[i-1] + f[i-k];
通过程序如下:(题目要求达到n有多少种变化,也就是将f[k] f[k+1] … f[n] 加起来)
#include <iostream>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
int n, k;
LL f[N], p = 1e9 + 7;
int main()
{
cin >> n >> k;
f[k] = 1;
LL ans = 2;
for (int i = k + 1; i <= n; i++) {
f[i] = (f[i-1] + f[i-k]) % p;
ans = (ans + f[i]) % p;
}
cout << ans;
return 0;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。