codeforces 125B Simple XML 正则表达式的使用

           因为,之前写了一篇有关正则表达式 的博客。在这周正巧碰到一个题在这里可以使用正则表达式在解决而且效果也是比较明显。先贴题:

B. Simple XML
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Let's define a string <x> as an opening tag, where x is any small letter of the Latin alphabet. Each opening tag matches a closing tag of the type </x>, where x is the same letter.

Tegs can be nested into each other: in this case one opening and closing tag pair is located inside another pair.

Let's define the notion of a XML-text:

  • an empty string is a XML-text
  • if s is a XML-text, then s'=<a>+s+</a> also is a XML-text, where a is any small Latin letter
  • if s1s2 are XML-texts, then s1+s2 also is a XML-text

You are given a XML-text (it is guaranteed that the text is valid), your task is to print in the following form:

  • each tag (opening and closing) is located on a single line
  • print before the tag 2 * h spaces, where h is the level of the tag's nestedness.
Input

The input data consists on the only non-empty string — the XML-text, its length does not exceed 1000 characters. It is guaranteed that the text is valid. The text contains no spaces.

Output

Print the given XML-text according to the above-given rules.

Examples
input
<a><b><c></c></b></a>
output
<a>
  <b>
    <c>
    </c>
  </b>
</a>
input
<a><b></b><d><c></c></d></a>
output
<a>
  <b>
  </b>
  <d>
    <c>
    </c>
  </d>
</a>

       在这个题中几乎不用看题直接看测试用例就能明白题意。输入一个字符串,然后安要求再次输出。(其中字符串的格式是'<x>'或'</x>'x可以是任意字母。)


      基本上这个题本质上不难,只要遍历一遍接收的字符串,然后做相应的记录就行了。但是,我的目的并不是仅仅是解开这个题,而是用一种更加简便的方法来解开它。(当然,如果你对语言使用比较熟练的话也能使代码非常精简。)所以,我就使用了我刚学会不就的正则表达式。


     接下来放代码,思路和解释都在代码里:(不过在这里没有解释使用到正则表达式的部分。所以,不了解的人建议先看下面网站。)

    http://blog.youkuaiyun.com/huangxy10/article/details/8117870

#include<iostream>
#include<cstdio>
#include<bits/stdc++.h>
using namespace std;

int main()
{
	char ch[5];          //使用ch来记录总的字符串中'x'或'/x'。 
	char s[1000];        //通过s来记录字符串中所有字母出现的顺序。 
	int list_space[1000];//通过list_space记录每个出现的字母中前面是否有'/'。 
	int space=0;         //在输出阶段表示出现'  '(两个空格。)的数量。 
	int count=0;         //负责记录字母的数量,也就是输出的行数。 
	while(scanf("%*[^(/a-z)]%[/a-z]",ch)!=EOF){
		//cout<<ch<<endl;//运行这个备注后会发现每次输入只接收字母和'/'。 
		if(ch[0]=='/'){  //当ch中接收的第一个字符是'/'时。 
			list_space[count]=-1;
			s[count++]=ch[1];
		}
		else{            //没有'/'时。 
			list_space[count]=+1;
			s[count++]=ch[0];
		}
	}
//如果连接codeforces的数据监测方式就明白为什么可以放在这里。
//在输入过程中就把所有的处理都记录下来后接下的就很方便了。
//  cout<<s<<endl;
//运行这个备注语句并输出后会明白s中记录的就是输入中的所有字符。 
	for(int j=0;j<count;j++){
		if(list_space[j]==-1) space--;
		for(int k=0;k<space;k++) cout<<"  ";
		if(list_space[j]==-1) printf("</%c>\n",s[j]);
		else{
			printf("<%c>\n",s[j]);
			space++;
		}
	}
}

          我在强调一下,我在这里展示的代码是使用的正则表达式后的代码。但是,其实对于这个题而言,并非一定要使用正则表达式。我在codeforces网站上也看到过很多没用正则表达式,而代码也是非常精简的。但是,在我知道有了正则之后我就好像有了强迫症一样。^.^!  我觉得一个好的程序员就用该具备各种装逼技巧。所以,作为菜鸟的我还是好好学习吧!





评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值