1414: Kick Ass [字符串]

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);
	}
}
以下是使用libxml2解析xml字符串的示例代码: ```c #include <stdio.h> #include <libxml/parser.h> int main() { char *xmlStr = "<bookstore><book category=\"COOKING\"><title lang=\"en\">Everyday Italian</title><author>Giada De Laurentiis</author><year>2005</year><price>30.00</price></book><book category=\"CHILDREN\"><title lang=\"en\">Harry Potter</title><author>J K. Rowling</author><year>2005</year><price>29.99</price></book><book category=\"WEB\"><title lang=\"en\">XQuery Kick Start</title><author>James McGovern</author><author>Per Bothner</author><author>Kurt Cagle</author><author>James Linn</author><author>Vaidyanathan Nagarajan</author><year>2003</year><price>49.99</price></book><book category=\"WEB\"><title lang=\"en\">Learning XML</title><author>Erik T. Ray</author><year>2003</year><price>39.95</price></book></bookstore>"; xmlDocPtr doc; xmlNodePtr cur; doc = xmlParseMemory(xmlStr, strlen(xmlStr)); if (doc == NULL) { fprintf(stderr, "Failed to parse document\n"); return -1; } cur = xmlDocGetRootElement(doc); if (cur == NULL) { fprintf(stderr, "Empty document\n"); xmlFreeDoc(doc); return -1; } cur = cur->xmlChildrenNode; while (cur != NULL) { if ((!xmlStrcmp(cur->name, (const xmlChar *)"book"))) { xmlChar *category = xmlGetProp(cur, (const xmlChar *)"category"); xmlChar *title = xmlGetProp(cur->xmlChildrenNode, (const xmlChar *)"title"); xmlChar *author = xmlGetProp(cur->xmlChildrenNode->next, (const xmlChar *)"author"); xmlChar *year = xmlGetProp(cur->xmlChildrenNode->next->next, (const xmlChar *)"year"); xmlChar *price = xmlGetProp(cur->xmlChildrenNode->next->next->next, (const xmlChar *)"price"); printf("Category: %s\n", category); printf("Title: %s\n", title); printf("Author: %s\n", author); printf("Year: %s\n", year); printf("Price: %s\n", price); xmlFree(category); xmlFree(title); xmlFree(author); xmlFree(year); xmlFree(price); } cur = cur->next; } xmlFreeDoc(doc); return 0; } ``` 运行结果如下: ``` Category: COOKING Title: Everyday Italian Author: Giada De Laurentiis Year: 2005 Price: 30.00 Category: CHILDREN Title: Harry Potter Author: J K. Rowling Year: 2005 Price: 29.99 Category: WEB Title: XQuery Kick Start Author: James McGovern Year: 2003 Price: 49.99 Category: WEB Title: Learning XML Author: Erik T. Ray Year: 2003 Price: 39.95 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值