Alternating Current
来源:互联网 发布:中国大数据企业排行榜 编辑:程序博客网 时间:2024/06/05 15:23
题目链接
题目大意:+、-分别表示两根线,如题图所示。+、-分别表示对应的线在上边。问是否能解开
思路:两个+或两个-相邻时,可以删除。一直这样下去,如果最后可以全部删掉,即可以解开,输出yes
问题:输入是线型,相邻的符号不一定都在端点,如果在中间删除则超时(也比较复杂,应该用list)
方法一:
int index[MAXN], x = 0, len = -1, cnt = 1;int main(){// freopen("in.txt", "r", stdin); char t, pre = 0; do { t = getchar(); if (t != pre) { if (cnt % 2 == 0) index[x++] = len; len++; cnt = 1; pre = t; } else cnt++; } while (t != '\n'); while (x > 1) { x--; if (index[x] - index[x - 1] < len - index[x]) { len -= (index[x] - index[x - 1]) * 2; x--; } else if (index[x] - index[x - 1] > len - index[x]) { len -= (len - index[x]) * 2 - 1; } else { len -= (len - index[x - 1]); x--; } } if ((x == 0 && len == 0) || (x == 1 && 2 * index[0] == len - 1)) puts("Yes"); else puts("No"); return 0;}
思路:将一段相同的符号缩减为一个数字,代表次段中符号出现的个数,如-++-缩减为121,偶数可以直接删除,同时要将两边的数字相加。
问题:直接使用这个规则和未转化的复杂度可能相当,如-+-+-+-+.....
简化:统计一下每个偶数出现的下标,一个偶数可以删除两边对应长度的奇数,之道碰到了偶数或者碰到了边界,分类讨论即可
简化后问题:能解决,但是问题比较复杂,编程难度高,容易出错
分析:将原问题转化(增加了难度,不必要),转化后也是从一段开始解决,将问题转化为线性,可以解决。那么,可以更简单一点,直接以题目中的输入入手,从一端开始,将遇到的相同符号 删去即可。
方法二:
stack<char> s;int main(){// freopen("in.txt", "r", stdin); char t; while ((t = getchar()) && t != '\n') { if (!s.empty() && s.top() == t) s.pop(); else s.push(t); } if (s.empty()) puts("Yes"); else puts("No"); return 0;}
第二个方法更本质,所以简单的多,不论是思维还是编程难度。
7 0
- Alternating Current
- Alternating Current
- Alternating Current
- B. Alternating Current----栈
- CodeForces 343B Alternating Current
- CodeForces 343B - Alternating Current
- Codeforces 344D - Alternating Current
- L - Alternating Current -链表贪心
- CodeForces - 344D Alternating Current (模拟题)
- codeforces 343B Alternating Current 【栈】
- CF#200 div2 D Alternating Current(stack)
- Codeforces 344D Alternating Current【模拟】
- Codeforces Round #200 (Div. 2)D. Alternating Current
- Codeforces Round #200 (Div. 2)344D Alternating Current(栈)
- Codeforces Round #200 (Div. 2)D. Alternating Current (堆栈)
- Alternating Sums
- fjnu 1281 Alternating Sums
- ZOJ 3813 Alternating Sum
- Could not open Hibernate Session for transaction, 数据库连接超时解决方法
- 协同推荐算法实践之Slope One的介绍
- [linux内存]linux内存学习(一)
- ncurses中经常使用的API
- INSERT ALL和INSERT FIRST语法
- Alternating Current
- 推荐系统相关算法(1):SVD
- SQL Server数据库入门学习总结
- iOS自定义tabbar,可以隐藏
- Android中asset文件夹和raw文件夹区别
- PRD-roadMap
- OCP-1Z0-053-V13.02-28题
- Jmeter 正则表达式提取器
- 存储过程语法及实例