CF 525A(Vitaliy and Pie-模拟)

A. Vitaliy and Pie
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

After a hard day Vitaly got very hungry and he wants to eat his favorite potato pie. But it's not that simple. Vitaly is in the first room of the house with n room located in a line and numbered starting from one from left to right. You can go from the first room to the second room, from the second room to the third room and so on — you can go from the (n - 1)-th room to the n-th room. Thus, you can go to room xonly from room x - 1.

The potato pie is located in the n-th room and Vitaly needs to go there.

Each pair of consecutive rooms has a door between them. In order to go to room x from room x - 1, you need to open the door between the rooms with the corresponding key.

In total the house has several types of doors (represented by uppercase Latin letters) and several types of keys (represented by lowercase Latin letters). The key of type t can open the door of type T if and only if t and T are the same letter, written in different cases. For example, key f can open door F.

Each of the first n - 1 rooms contains exactly one key of some type that Vitaly can use to get to next rooms. Once the door is open with some key, Vitaly won't get the key from the keyhole but he will immediately run into the next room. In other words, each key can open no more than one door.

Vitaly realizes that he may end up in some room without the key that opens the door to the next room. Before the start his run for the potato pie Vitaly can buy any number of keys of any type that is guaranteed to get to room n.

Given the plan of the house, Vitaly wants to know what is the minimum number of keys he needs to buy to surely get to the room n, which has a delicious potato pie. Write a program that will help Vitaly find out this number.

Input

The first line of the input contains a positive integer n (2 ≤ n ≤ 105) — the number of rooms in the house.

The second line of the input contains string s of length n - 2. Let's number the elements of the string from left to right, starting from one.

The odd positions in the given string s contain lowercase Latin letters — the types of the keys that lie in the corresponding rooms. Thus, each odd position i of the given string s contains a lowercase Latin letter — the type of the key that lies in room number (i + 1) / 2.

The even positions in the given string contain uppercase Latin letters — the types of doors between the rooms. Thus, each even positioni of the given string s contains an uppercase letter — the type of the door that leads from room i / 2 to room i / 2 + 1.

Output

Print the only integer — the minimum number of keys that Vitaly needs to buy to surely get from room one to room n.

Sample test(s)
input
3
aAbB
output
0
input
4
aBaCaB
output
3
input
5
xYyXzZaZ
output
2

直接模拟


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (1000000+10) 
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int n;
char s[MAXN];
int b[MAXN]={0};
int main()
{
//	freopen("A.in","r",stdin);
	int ans=0;
	scanf("%d",&n);
	scanf("%s",s);
	int len=strlen(s);
	Rep(i,len)
	{
		if ('a'<=s[i]&&s[i]<='z') b[s[i]]++;
		else
		{
			int t=(s[i]-'A'+'a');
			if (b[t]>0) b[t]--;
			else ans++;
			
			
		}
		
		
	}
	
	cout<<ans<<endl;
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 您好,我是ChitGPT,一个类ChatGPT的AI,很高兴为您解答问题。针对您的问题,以下是我的回答。 针对算数表达式的计算器,我们可以按照以下步骤来实现代码: 1. 首先,我们需要将算数表达式转换成后缀表达式,这样才能方便地进行计算。可以借助栈来实现。具体实现方法请参考以下链接:https://www.jianshu.com/p/d114b7920ec3。 2. 转换后缀表达式后,我们可以借助栈来实现运算。对于数字,直接入栈;对于操作符,弹出两个操作数进行运算,将结果压入栈中。具体实现方法请参考以下链接:https://www.jianshu.com/p/ff525a631c77。 3. 最后,弹出栈顶元素即可得到表达式的结果。 以下是基于以上思路的C++代码实现(注释已添加): ```c++ #include <iostream> #include <stack> #include <string> using namespace std; int evaluate(string exp) { stack<int> s; // 定义栈 for (int i = 0; i < exp.size(); i++) { if (exp[i] == ' ') continue; // 跳过空格 else if (isdigit(exp[i])) { // 处理数字 int num = 0; while (isdigit(exp[i])) { // 将连续的数字合并 num = 10 * num + (exp[i] - '0'); i++; } i--; // 因为上面i已经移动到了不是数字的位置,因此需要回退一位 s.push(num); // 将数字压入栈中 } else { // 处理运算符 int num1 = s.top(); s.pop(); int num2 = s.top(); s.pop(); switch (exp[i]) { case '+': s.push(num2 + num1); break; case '-': s.push(num2 - num1); break; case '*': s.push(num2 * num1); break; case '/': s.push(num2 / num1); break; } } } return s.top(); // 弹出栈顶元素,即为表达式的结果 } int main() { string exp = "1 + 2 * 3 - ( 4 / 5 )"; int res = evaluate(exp); cout << res << endl; return 0; } ``` 以上是算数表达式求值的简单计算器的实现。感谢您提供的问题,如果还有其他问题需要解答,请随时提出,我将尽力为您提供帮助。 ### 回答2: 以下是一个简单的算术四则运算表达式求值的计算器的设计: ```python # 设计计算器函数evaluate_expression,用来求算术表达式的值 def evaluate_expression(expression): stack = [] # 创建一个栈来保存操作数和操作符 # 遍历表达式的每个字符 for char in expression: # 如果是左括号,直接入栈 if char == '(': stack.append(char) # 如果是右括号,就从栈中取出操作符和操作数进行计算,直到遇到左括号为止 elif char == ')': while stack[-1] != '(': calculate(stack) stack.pop() # 弹出左括号 # 如果是操作符,就将其放入栈中 elif char in ['+', '-', '*', '/']: stack.append(char) # 如果是数字,就将其解析为整数并将其放入栈中 else: stack.append(int(char)) # 最后计算栈中剩余的操作符和操作数,得到最终的结果 while len(stack) > 1: calculate(stack) return stack[0] # 返回计算结果 # 定义一个辅助函数用来计算两个数的运算结果,并将结果放回栈中 def calculate(stack): operator = stack.pop() operand2 = stack.pop() operand1 = stack.pop() if operator == '+': stack.append(operand1 + operand2) elif operator == '-': stack.append(operand1 - operand2) elif operator == '*': stack.append(operand1 * operand2) elif operator == '/': stack.append(operand1 / operand2) # 在main函数中进行测试 def main(): expression = "3 + 2 * (4 - 1)" # 测试表达式 result = evaluate_expression(expression) print(f"表达式 {expression} 的值为:{result}") # 调用main函数进行测试 if __name__ == "__main__": main() ``` 这个计算器的基本原理是利用栈数据结构来实现计算过程。对于给定的算术表达式,我们从左往右遍历每个字符,并按照以下规则进行处理: 1. 如果遇到左括号,直接入栈。 2. 如果遇到右括号,就从栈中取出操作符和操作数进行运算,直到遇到左括号为止。 3. 如果遇到操作符,就将其放入栈中。 4. 如果遇到数字,就将其解析为整数并放入栈中。 在遍历完整个表达式后,我们还需要计算栈中剩余的操作符和操作数,得到最终的结果。 在main函数中,我们可以调用evaluate_expression函数来进行测试。示例中的测试表达式是"3 + 2 * (4 - 1)",运行结果为11。 ### 回答3: 下面是一个简单的算术四则运算表达式求值的计算器的代码: ```c++ #include <iostream> #include <stack> #include <string> using namespace std; // 判断运算符的优先级 int getPriority(char op) { if (op == '+' || op == '-') return 1; else if (op == '*' || op == '/') return 2; else return 0; } // 计算两个数字的运算结果 int calculate(int num1, int num2, char op) { if (op == '+') return num1 + num2; else if (op == '-') return num1 - num2; else if (op == '*') return num1 * num2; else return num1 / num2; } // 解析并计算表达式的值 int calculateExpression(string expression) { stack<int> numStack; stack<char> opStack; int num = 0; // 用来记录当前数字 for (int i = 0; i < expression.length(); i++) { char c = expression[i]; if (isdigit(c)) { // 如果是数字,更新当前数字 num = num * 10 + (c - '0'); } else if (c == '(') { // 如果是左括号,将运算符压入栈 opStack.push(c); } else if (c == ')') { // 如果是右括号,计算括号内的表达式 while (!opStack.empty() && opStack.top() != '(') { int num2 = numStack.top(); numStack.pop(); int num1 = numStack.top(); numStack.pop(); char op = opStack.top(); opStack.pop(); int result = calculate(num1, num2, op); numStack.push(result); } opStack.pop(); // 弹出左括号 } else { // 如果是运算符,计算之前可能存在的更高优先级的运算符 while (!opStack.empty() && getPriority(c) <= getPriority(opStack.top())) { int num2 = numStack.top(); numStack.pop(); int num1 = numStack.top(); numStack.pop(); char op = opStack.top(); opStack.pop(); int result = calculate(num1, num2, op); numStack.push(result); } opStack.push(c); // 将当前运算符压入栈 } if (i == expression.length() - 1 || expression[i+1] == '+' || expression[i+1] == '-' || expression[i+1] == '*' || expression[i+1] == '/') { // 如果当前字符之后是一个新的运算符或者是表达式的结尾,则将当前数字压入栈 numStack.push(num); num = 0; // 重置当前数字 } } // 计算剩余的表达式 while (!opStack.empty()) { int num2 = numStack.top(); numStack.pop(); int num1 = numStack.top(); numStack.pop(); char op = opStack.top(); opStack.pop(); int result = calculate(num1, num2, op); numStack.push(result); } return numStack.top(); // 返回最终结果 } int main() { string expression; cout << "请输入要计算的表达式:" << endl; getline(cin, expression); int result = calculateExpression(expression); cout << "计算结果为:" << result << endl; return 0; } ``` 该计算器首先使用两个栈,一个存储数字,一个存储运算符。然后从左到右遍历表达式,分别处理数字、括号和运算符。当遇到数字时,将其累积为一个完整数字;当遇到左括号时,将其压入运算符栈;当遇到右括号时,从运算符栈中弹出运算符并计算数字栈中对应数量的数字,并将结果压入数字栈;当遇到运算符时,分别处理优先级高于或等于该运算符的运算符,并将当前运算符压入运算符栈。最后,计算剩余的表达式,并返回数字栈中的最终结果。最后,通过 main 函数进行测试,用户输入要计算的表达式,然后调用 calculateExpression 函数计算结果,并将结果打印输出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值