1414: Kick Ass [字符串]
时间限制: 1 Sec 内存限制: 128 MB提交: 112 解决: 33 统计
题目描述
你玩过一个叫做"Kick Ass - Destroy the web"的游戏吗?如果你想玩的话
当然,你没有玩过也没关系,这个游戏是通过js来加载一个飞船,让你可以通过控制它发射子弹来摧毁网页上的元素,从而获得分数。
我们知道HTML是超文本标记语言,简单地说就是通过一些标签来指明元素的展示方式,比如<p></p>
就可用作表明这是一个段落,<div></div>
表明这是一个区块。
假设有一个网页只含有<p></p>
、<div></div>
两种标记,其中<p><p>
中只含有数字,摧毁<p></p>
标记可获得其中各个数字之和的分数,摧毁<div></div>
可获得其中包含的所有<p></p>
分数与包含的<div></div>
分数二倍之和的分数。
现在给你一个网页,你能否计算出完全摧毁它可得到的分数。
为了简化难度,你可以假设:
- 所给网页的字符只有数字和标签
- 所给网页的标签都正确匹配
- 保证所给网页中的数字都在
<p></p>
标签内 <p></p>
标签内不会含有标签<div></div>
标签内可能含有<p></p>
标签和<div></div>
标签
输入
多组测试数据,请处理到文件结束。
每组占一行,有一个长度不大于1000的字符串代表所给网页。
输出
请输出完全摧毁此网页可获得的分数。
样例输入
<div><p>123</p><div><p>123</p></div></div>
样例输出
18
这个题的意思是说 当遇到<div></div>时,只要外围再次存在<div>的话,就要使这个div里面的包含的数扩大2倍再次存入一个栈里,每个div 里的数,他们都是用的-1进行隔开
以用来进行扩大2 倍,对于<p>来处理的话只需将里面的数字的和存进这个栈里,注意在求和之后使其和的值变为0.
#include<stdio.h> #include<stack> #include<string.h> using namespace std; int main() { char str[1010]; while(~scanf("%s",str)) { stack<int > s; int shu = 0, ans = 0, sum = 0; int len = strlen(str); for(int i = 0; i < len; i++){ if(str[i] == '<'&&str[i+1] == 'd') { shu ++; s.push(-1); } else if(str[i] == '<'&&str[i+1] == '/'&&str[i+2] == 'd') { while(!s.empty()&&s.top() != -1) { ans +=s.top(); s.pop(); } s.pop(); //弹出-1 shu --; if(shu) s.push(ans*2); else s.push(ans); ans = 0; } else if(str[i] == '<'&&str[i+1] == 'p' ) { i += 3; while(str[i] != '<') { ans += str[i] - '0'; i++; } s.push(ans); ans = 0; } } while(!s.empty()) { sum += s.top(); s.pop(); } printf("%d\n", sum); } }