icpc:problems:usaco23jan_moo_operations_b
problems | |
---|---|
名称 | Moo Operations B |
题目编号 | USACO23JAN_B3 |
题目链接 | luogu.com.cn/… |
来源 | USACO |
算法分类 | 特判, 思维 |
难易程度 | 容易 |
Moo Operations B
想法
特殊判断小王子。
- 当长度小于3时,不行;
- 当长度等于3,但是中间的字符不是'O',不行;
- 当长度大于4,必须在字符中间寻找'O',因为3个的中间是没有办法通过操作1实现的。
找到'O',检查'O'的两边,找一个最优秀的方案。其余的字符通过操作1删除。
代码实现
#include<cstdio> #include<cstring> int main() { int n; char line[110]; scanf("%d\n", &n); for (int t = 1; t <= n; ++t) { scanf("%s", line); int len = strlen(line), ans = 0; if(len < 3 || (len == 3 && line[1] != 'O')){ printf("-1\n"); continue; } if(len == 3) { if(line[0] != 'M') ans++; if(line[2] != 'O') ans++; printf("%d\n", ans); continue; } ans = 4; for (int i = 1; i < len-1; ++i) { if(line[i] == 'O'){ int temp = 0; if(line[i-1] != 'M') temp++; if(line[i+1] != 'O') temp++; if(temp < ans) ans = temp; } } if(ans == 4) printf("-1\n"); else printf("%d\n", ans + (len - 3)); } return 0; }
/app/www/public/data/pages/icpc/problems/usaco23jan_moo_operations_b.txt · 最后更改: 2023/05/23 11:04 由 温婕莺