UVA_112_Tree Summing

本文介绍了一个使用链表和栈实现的数据结构应用案例。通过递归计算链表节点之和,并利用栈来辅助处理括号匹配及数值计算,最终判断链表节点之和是否等于给定的目标值。
#include<iostream>
#include<sstream>
#include<vector>
#include<string>
#include<stack>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;
using std::stringstream;
using std::stack;
const int MAX = 2147483647;
typedef struct NodeType
{
	int value;
	NodeType *pre = nullptr;
	NodeType *next = nullptr;
	bool flag = true;
}NodeType, *Node;
int result(Node p)
{
	if (p)
	{
		return p->value + result(p->pre);
	}
	else
	{
		return 0;
	}
}
bool judge(stack<Node>&leaf, const int &sum)
{
	while (!leaf.empty())
	{
		auto p = leaf.top(); leaf.pop();
		if (sum == result(p))
		{
			return true;
		}
	}
	return false;
}
void scan(stack<Node>&leaf)
{
	stack<char>bracket;
	stack<Node>value;
	char ch;
	int result = MAX;
	int sign = 1;
input:    while (cin >> ch)
{
	if (ch == '-')
	{
		sign = -1;
	}
	else if (ch == '(' || ch == ')')
	{
		if (ch == '(')
		{
			if (!value.empty())
			{
				if (result != MAX)
				{
					//value.emplace(nullptr,result,value.top(),true);
					auto p = new NodeType;
					p->value = result;
					p->pre = value.top();
					p->next = nullptr;
					p->flag = true;
					value.top()->next = p;
					value.push(p);
					result = MAX;
					sign = 1;
				}
			}
			else
			{
				if (result != MAX)
				{
					auto p = new NodeType;
					p->value = result;
					p->pre = nullptr;
					p->next = nullptr;
					p->flag = true;
					value.push(p);
					result = MAX;
					sign = 1;
				}
			}
			bracket.push(ch);
		}
		else
		{
			bracket.pop();
			if (bracket.empty() && value.empty())
			{
				break;
			}
			if (value.top()->flag)
			{
				value.top()->flag = false;
			}
			else
			{
				if (!value.top()->next)
				{
					leaf.push(value.top());
				}
				value.pop();
			}
		}
	}
	//如果是数字
	else
	{
		if (result == MAX)
		{
			result = 0;
		}
		result *= 10;
		if (sign > 0)
		{
			result += ch - '0';
		}
		else
		{
			result -= ch - '0';
		}
	}
}
}
void print(stack<Node>&leaf, const int &sum)
{
	if (judge(leaf, sum))
	{
		cout << "yes" << endl;
	}
	else
	{
		cout << "no" << endl;
	}
}
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int sum;
	while (cin >> sum)
	{
		stack<Node> leaf;
		scan(leaf);
		print(leaf, sum);
	}
	return 0;
}

### Rotated Bounding Box Single IoU Implementation and Explanation In computer vision tasks involving object detection, especially when dealing with rotated objects such as in aerial imagery or specific orientations of items within an image, traditional axis-aligned bounding boxes may not provide accurate representations. For this reason, calculating Intersection over Union (IoU) for rotated bounding boxes becomes crucial. #### Definition of Rotated Bounding Boxes A rotated bounding box is defined by its center coordinates \((cx, cy)\), width \(w\), height \(h\), and rotation angle \(\theta\) relative to the horizontal axis[^1]. This representation allows more precise fitting around non-axis-aligned objects compared to standard rectangular bounds. #### Calculation Methodology To compute IoU between two rotated rectangles, one approach involves converting each rectangle into a polygon format using their respective parameters. The intersection area can then be calculated through geometric operations on these polygons while union areas are derived from subtracting intersections from summed individual areas: \[ IOU = \frac{Area_{Intersection}}{Area_{Union}} \] Where: - \( Area_{Intersection} \): Calculated via clipping algorithms like Sutherland-Hodgman. - \( Area_{Union} \): Derived from summing up both shapes' areas minus overlapping section. This process requires careful handling due to potential complexities introduced by rotations which might lead to self-intersecting cases during conversion steps. For practical implementations, libraries such as `shapely` offer built-in functions that simplify working with geometrical entities including computing intersections efficiently without manually implementing low-level geometry processing routines. ```python from shapely.geometry import Polygon def calculate_rotated_iou(box_a_params, box_b_params): """ Calculate IoU given parameter sets describing two rotated bounding boxes Parameters: box_a_params ((float,float,float,float,float)): Tuple containing cx,cy,w,h,angle for first bbox box_b_params ((float,float,float,float,float)): Same structure but for second bbox Returns: float: Value representing computed IoU score ranging [0,1] """ # Convert parametric form into Polygons poly_a = create_polygon_from_box(*box_a_params) poly_b = create_polygon_from_box(*box_b_params) inter_area = poly_a.intersection(poly_b).area union_area = poly_a.union(poly_b).area return inter_area / union_area if union_area != 0 else 0 def create_polygon_from_box(cx, cy, w, h, theta): """Helper function creating Shapely Polygon instances.""" corners = [ (-w/2,-h/2), (+w/2,-h/2), (+w/2,+h/2), (-w/2,+h/2)] transformed_corners = [(corner[0]*cos(theta)-corner[1]*sin(theta)+cx, corner[0]*sin(theta)+corner[1]*cos(theta)+cy) for corner in corners] return Polygon(transformed_corners) ``` The provided code snippet demonstrates how to implement IoU calculation specifically tailored towards scenarios where bounding boxes have arbitrary angles associated with them. Utilizing external packages helps streamline development efforts allowing focus on higher level logic rather than intricate details involved in manipulating spatial data structures directly. --related questions-- 1. How do different types of transformations affect accuracy metrics used in evaluating models? 2. What challenges arise when applying conventional evaluation measures to specialized datasets? 3. Can you explain alternative methods besides IoU for assessing performance in object detection systems? 4. In what ways could pre-processing techniques improve alignment between predicted and ground truth annotations?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值