HDU ACM 4041 Eliminate Witches! 队列和栈模拟DFS

分析:直接模拟即可,这里用队列记录访问过的点,栈记录父节点。另外要注意的是在strlen(str)计算字符串的时候要预先计算出来保存在变量中,for直接用,如果for循环直接调用strlen,那么每次都会重新计算,该題字符串的数据量很大,就会存在大量的无用计算,还导致了一次TLE,唉!以前没注意到这里。

#include<iostream>
#include<vector>
#include<queue>
#include<stack>
using namespace std;

char room[50005][12];
queue<int> q;
stack<int> s;
char str[1000005];

int main()
{
	int T,i,sum,j,len;

	scanf("%d",&T);
	while(T--)
	{
		scanf("%s",str);
		sum=0;
		j=0;

		len=strlen(str);   //事先计算,避免每次计算,数据大时就会影响到效率
		for(i=0;i<len;i++) //队列q用于保存路径上经过的节点,插入时均插入当前非字母字符前面的节点
		{
			if(str[i]>='a' && str[i]<='z')     //栈用于保存进入的路径
				room[sum][j++]=str[i];
			else
			{
				if(j!=0)                //处理两个连续的非字母情况。
				{
			    	room[sum++][j++]='\0';
			    	j=0;
				}
				if(str[i]=='(')         //进入节点
				{
					s.push(sum);
					q.push(sum);
				}
				else if(str[i]==',')
				{
					if(str[i-1]!=')')         //退回父节点
					{
						q.push(sum);
						q.push(s.top());
					}
			     	else                     //退到该节点的父节点的父节点
					{
						q.push(s.top());
						s.pop();
						q.push(s.top());
					}
				}
				else if(str[i]==')')
				{
					if(str[i-1]!=')')    //记录前一个节点
						q.push(sum);
					else                   //出栈
					{
				    	q.push(s.top());
						s.pop();
					}
					if(s.size()==1)         //回到根节点
					{
						q.push(s.top());    //插入根节点
						s.pop();         //之后栈为空
					}
				}
			}
		}
		if(sum==0)
			printf("1\n%s\n",str);
		else
		{
			printf("%d\n",sum);
			for(i=0;i<sum;i++)
				printf("%s\n",room[i]);

			while(q.size()!=1)
			{
				printf("%d ",q.front());
				q.pop();
				printf("%d\n",q.front());
			}
			q.pop();     //退出根节点
		}
		printf("\n");
	}
    return 0;
}
时间: 2024-07-31 22:08:02

HDU ACM 4041 Eliminate Witches! 队列和栈模拟DFS的相关文章

Code POJ - 1780(栈模拟dfs)

题意: 就是数位哈密顿回路 解析: 是就算了...尼玛还不能直接用dfs,得手动开栈模拟dfs emm...看了老大半天才看的一知半解 #include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <cctype> #include <set> #include <vector&g

HDU 4041 Eliminate Witches! (栈的模拟)

Eliminate Witches! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1234    Accepted Submission(s): 461 Problem Description Kaname Madoka is a Magical Girl(Mahou Shoujo/Puella Magi). The duty of

HDU 4041 Eliminate Witches! --模拟

题意: 给一个字符串,表示一颗树,要求你把它整理出来,节点从1开始编号,还要输出树边. 解法: 模拟即可.因为由括号,所以可以递归地求,用map存对应关系,np存ind->name的映射,每进入一层括号,使father = now, 遇到右括号')',则father = fa[father],用vector存每个节点的子节点,然后最后dfs输出即可. 代码: #include <iostream> #include <cstdio> #include <cstring&

HDU ACM 1057 A New Growth Industry 简单模拟

题意:给一个天数N,求20*20方阵内细菌的变化情况.每次变化加上一个d[k],d数组有给定的16个数.k是某个格子它本身加上它上下左右的四个数. 简单模拟题. 分析: #include<iostream> using namespace std; int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}}; char den[]=".!X#"; int D[16]; int map[20][20],tmp[20][20]; void Fun() {

【作业】用栈模拟dfs

题意:一个迷宫,起点到终点的路径,不用递归. 题解: #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string> #include<string.h> #include<stack> #include<iostream> #include<map> using namespace std; const int m

HDU 1702 ACboy needs your help again! (栈和队列的模拟)

ACboy needs your help again! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3164    Accepted Submission(s): 1655 Problem Description ACboy was kidnapped!! he miss his mother very much and is ve

HDU 1242 &amp;&amp; ZOJ 1649( BFS (队列 || 优先队列)).

~~~~ 突然发现一篇搜索的题目都有写.昨天发现道bfs题目,HDU上AC, ZOJ上WA.不得不说HDU上的数据之水.. 今天早起刷题有了思路,并用队列和单调队列都写了一遍,0MS飘过~~ ~~~~ 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=649 ~~~~ 首先有坑的地方是friends,对嘛,朋友有很多,ang

HDU 1022 Train Problem I (STL 栈模拟)

Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 30420    Accepted Submission(s): 11492 Problem Description As the new term comes, the Ignatius Train Station is very busy nowaday

编程题目: 两个队列实现栈(Python)

感觉两个队列实现栈 比 两个栈实现队列 麻烦 1.栈为空:当两个队列都为空的时候,栈为空 2.入栈操作:当队列2为空的时候,将元素入队到队列1:当队列1位空的时候,将元素入队到队列2: 如果队列1 和 队列2 都为空的时候,那就选择入队到队列1. 3.出队操作:当两个队列都为空的时候,引发错误"栈为空": 当队列2位空的时候,如果队列1中只有一个元素,则直接将队列1中的元素出队: 如果队列1不止一个元素的时候,就将队列1的元素出队然后入队到队列2,知道队列1中只有一个元素,然后将队列1