4 条题解
-
3
#include <bits/stdc++.h> using namespace std; int main() { int s,t,w,count=0,si; string str; cin>>s>>t>>w>>str; while(count<5) { for(int i=w-1; i>=0; i--) { si=str[i]-'a'+w-i;//判断是否到达边界 if(si<t) { str[i]=str[i]+1;//第一个数向后推 for(int j=i+1; j<w; j++) str[j]=str[j-1]+1;//第二个及以后的数向前推,不+1字母会重叠 count++; cout<<str<<endl; break; } } } return 0; }//已AC
-
3
题前吐槽
你说Jam这疯子瞎搞啥……
再说了Jam瞎疯为啥我们编程序……
结论:CCF……
题解
思路1
这道题可以用dfs解决。
我们用数组 表示 数,下标从 开始。
我们首先来看递归的边界:
- 因为最多只要输出 个字符串(事实上该算法也因此效率较高,不必担心超时问题),所以计算 个之后就可以直接结束了,因此
if (step == 6) return;
。 - 宏观上讲,我们是把a数组的元素一个一个往后移,所以当第一个元素都不可以移动时,自然程序也就结束了,
if (pos == 0) return;
。
然后来看状态转移:
- 如果位置为 的 数组元素可以向后移动,那么我们将其移动一位,同时因为要保证 数从小到大,我们把 后面的元素往前移动,这不难理解,前面元素的“权”大于后面元素的“权”,如果要使得序列单调递增,就必须这么做。
- 如果位置为 的元素不能移动,那么我们就移动位置为 的元素。
#include <bits/stdc++.h> using namespace std; int s, t, w, c, a[30], cnt = 0; string q; void dfs(int pos, int step) { // 边界 if (step == 6) return; if (pos == 0) return; if (a[pos] < (t + 'a' - 1) && a[pos] < a[pos + 1] - 1) // 能增加 { a[pos]++; // 增加 for (int i = pos + 1; i <= w; i++) // 后面的依次增加 a[i] = a[i - 1] + 1; for (int i = 1; i <= w; i++) putchar(a[i]); printf("\n"); dfs(w, step + 1); // 下一个 } else dfs(pos - 1, step); // 不能就向前转 } int main() { scanf("%d%d%d", &s, &t, &w); cin >> q; // scanf难以输入string for (int i = 1; i <= w; i++) a[i] = q[i - 1]; a[w + 1] = 0x7f; dfs(w, 1); // dfs return 0; }
思路2
直接模拟也可以, 次 。
#include <bits/stdc++.h> using namespace std; int s, t, w; char a[30]; string q; void add() { for (int i = w; i >= 1; i--) // 每一位 if (a[i] - 96 < t - w + i) // 这一位可以满足后面一次增加了 { a[i]++; for (int j = i + 1; j <= w; j++) a[j] = a[j - 1] + 1; // 逐位增加 for (int j = 1; j <= w; j++) putchar(a[j]); // 输出 printf("\n"); return; } } int main() { scanf("%d%d%d", &s, &t, &w); cin >> q; // scanf难以输入string for (int i = 1; i <= w; i++) a[i] = q[i - 1]; // 转进a数组 for (int i = 1; i <= 5; i++) add(); // 5次+1,如果不足5次,后面几次循环完了不会输出 return 0; }
#include <bits/stdc++.h> using namespace std; int s, t, w, c, a[30], cnt = 0; string q; void dfs(int pos, int step) { if (step == 6) return; if (pos == 0) return; if (a[pos] < (t + 'a' - 1) && a[pos] < a[pos + 1] - 1) { a[pos]++; for (int i = pos + 1; i <= w; i++) a[i] = a[i - 1] + 1; for (int i = 1; i <= w; i++) putchar(a[i]); printf("\n"); dfs(w, step + 1); } else dfs(pos - 1, step); } int main() { scanf("%d%d%d", &s, &t, &w); cin >> q; for (int i = 1; i <= w; i++) a[i] = q[i - 1]; a[w + 1] = 0x7f; dfs(w, 1); return 0; }
#include <bits/stdc++.h> using namespace std; int s, t, w; char a[30]; string q; void add() { for (int i = w; i >= 1; i--) if (a[i] - 96 < t - w + i) { a[i]++; for (int j = i + 1; j <= w; j++) a[j] = a[j - 1] + 1; for (int j = 1; j <= w; j++) putchar(a[j]); printf("\n"); return; } } int main() { scanf("%d%d%d", &s, &t, &w); cin >> q; for (int i = 1; i <= w; i++) a[i] = q[i - 1]; for (int i = 1; i <= 5; i++) add(); return 0; }
- 因为最多只要输出 个字符串(事实上该算法也因此效率较高,不必担心超时问题),所以计算 个之后就可以直接结束了,因此
-
1
域:比赛真题
题目:CSP 2021年真题
在这道题里发那道题的题解是因为:2021年真题不让发题解
忘各位大佬谅解!
实在是没办法了,CSP 2021年真题里我没权限创建题解代码(客观题)
type: objective # 表明该题为客观题 answers: # 列举出每一题的正确选项与对应的得分 '1': ['D', 2] '2': ['B', 2] '3': ['A', 2] '4': ['C', 2] '5': ['D', 2] '6': ['D', 2] '7': ['C', 2] '8': ['A', 2] '9': ['B', 2] '10': ['B', 2] '11': ['B', 2] '12': ['A', 2] '13': ['C', 2] '14': ['B', 2] '15': ['B', 2] '16': ['B', 1.5] '17': ['B', 1.5] '18': ['B', 1.5] '19': ['A', 1.5] '20': ['B', 1.5] '21': ['B', 3] '22': ['B', 1.5] '23': ['A', 1.5] '24': ['A', 1.5] '25': ['B', 3] '26': ['B', 3] '27': ['C', 3.5] '28': ['A', 1.5] '29': ['B', 2] '30': ['B', 2] '31': ['A', 3] '32': ['C', 3] '33': ['C', 4] '34': ['D', 3] '35': ['C', 3] '36': ['C', 3] '37': ['D', 3] '38': ['B', 3] '39': ['B', 3] '40': ['D', 3] '41': ['C', 3] '42': ['B', 3] '43': ['D', 3]
-
0
#include <iostream> #include <string> #include <cmath> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int s,t,w; char a[27]; int main() { for (int i = 1;i <= 26;i++) { a[i] = 'a' + i - 1; } cin >> s >> t >> w; char str[w + 1] = { }; for (int i = 1;i <= w;i++) { cin >> str[i]; } for (int z = 1;z <= 5;z++) { // 5(w) 10(t) // 4 9 for (int i = w;i >= 1;i--) { //cout << i << "," << str[i] << "," << a[t-w+i] << endl; if (str[i] != a[t-w+i]) { str[i]++; for (int j = i + 1;j <= w;j++) { str[j] = str[i] + j - i; } break; } } for (int tmp = 1;tmp <= w;tmp++) { cout << str[tmp]; } cout << endl; } return 0; }//真难啊 呜呜呜
- 1
信息
- ID
- 1688
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 3
- 标签
- 递交数
- 178
- 已通过
- 100
- 上传者