题目传送:Codeforces Round #302 (Div. 2)
A. Set of Strings
思路:注意开头字母都不相同
AC代码:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <vector> #include <map> #include <set> #include <deque> #include <cctype> #define LL long long #define INF 0x7fffffff using namespace std; char s[105]; int flag[26]; int f[26]; int main() { int k; scanf("%d", &k); scanf("%s", s); int len = strlen(s); memset(flag, 0, sizeof(flag)); for(int i = 0; i < len; i ++) { flag[s[i] - 'a'] = 1; } int cnt = 0; for(int i = 0; i < 26; i ++) { cnt += flag[i]; } if(cnt < k) { cout << "NO" << endl; } else { memset(f, 0, sizeof(f)); char cur = s[0]; cout << "YES" << endl; printf("%c", s[0]); f[s[0] - 'a'] = 1; int tt = 1; for(int i = 1; i < len; i ++) { if(s[i] != cur && tt != k && f[s[i] - 'a'] == 0) { printf("\n%c", s[i]); f[s[i] - 'a'] = 1; tt ++; cur = s[i]; } else printf("%c", s[i]); } } return 0; }
B. Sea and Islands
思路:构造,给你一个n*n的地图,看地图上能不能有k个岛,岛是由一个联通块组成的(相邻的即为一个联通块)
AC代码:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <vector> #include <map> #include <set> #include <deque> #include <cctype> #define LL long long #define INF 0x7fffffff using namespace std; int main() { int n, k; cin >> n >> k; int sum = n & 1 ? (n * n + 1) / 2 : n * n / 2;//最多可以有这么多个岛 if(k > sum) { cout << "NO" << endl; } else { cout << "YES" << endl; int t = 0; for(int i = 1; i <= n; i++, printf("\n")) { for(int j = 1; j <= n; j ++) { if(i & 1 && t != k) { if(j & 1) { printf("L"); t ++; } else { printf("S"); } } else if(!(i & 1) && t != k){ if(j & 1) { printf("S"); } else { printf("L"); t ++; } } else if(t == k) { printf("S"); } } } } return 0; }
C. Writing Code
思路:简单DP,完全背包
AC代码:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <vector> #include <map> #include <set> #include <deque> #include <cctype> #define LL long long #define INF 0x7fffffff using namespace std; LL dp[505][505]; LL a[505]; int main() { int n, m, b, mod; scanf("%d %d %d %d", &n, &m, &b, &mod); for(int i = 1; i <= n; i ++) { scanf("%d", &a[i]); } dp[0][0] = 1; for(int i = 1; i <= n; i ++) { for(int j = 1; j <= m; j ++) { for(int k = 0; k <= b; k ++) { if(k >= a[i]) dp[j][k] += dp[j-1][k - a[i]]; dp[j][k] %= mod; } } } LL ans = 0; for(int i = 0; i <= b; i ++) { ans += dp[m][i]; ans %= mod; } cout << ans << endl; return 0; }
时间: 2024-10-04 14:39:25