题不难,但是太坑了。。
1.【深基15.例2】寄包柜
emmm,因为不想用vector,用map代替了8
#include<bits/stdc++.h>
using namespace std;
map<int, map<int,int> >EVA;
int main(){
int m,n;
cin>>m>>n;
for(int i=1;i<=n;i++){
int op;
cin>>op;
if(op==1){
int a,b,c;
cin>>a>>b>>c;
EVA[a][b]=c;
}
else{
int a,b;
cin>>a>>b;
cout<<EVA[a][b]<<endl;
}
}
return 0;
}
2.括号序列。
思路:结构体的栈。
代码:
#include<bits/stdc++.h>
using namespace std;
bool book[1001];
struct EVA{
int id;
char ce;
}Z[1000];;
int cnt=0;
int main(){
string str;
cin>>str;
int len=str.length();
for(int i=0;i<len;i++){
if(str[i]=='(' || str[i]=='[')Z[++cnt].id=i,Z[cnt].ce=str[i];
else{
if(str[i]==')'&& Z[cnt].ce=='('){
book[Z[cnt].id]=1;
book[i]=1;
cnt--;
}
else if(str[i]==']'&& Z[cnt].ce=='['){
book[Z[cnt].id]=1;
book[i]=1;
cnt--;
}
}
}
for(int i=0;i<len;i++){
if(book[i]==0){
if(str[i]=='(')cout<<str[i]<<")";
else if(str[i]==')')cout<<"("<<str[i];
else if(str[i]=='[')cout<<str[i]<<"]";
else cout<<"["<<str[i];
}
else cout<<str[i];
}
return 0;
}
3.后缀表达式
思路:注意有十位百位。。。
还有/
代码:
#include<bits/stdc++.h>
using namespace std;
int sat[10000];
int cnt=0;
int main(){
string str;
cin>>str;
int len=str.length();
int js=0;
for(int i=0;i<len;i++){
if(str[i]=='.'){
sat[++cnt]=js;
js=0;
}
else if(str[i]>='0' && str[i]<='9'){
js*=10;
js+=str[i]-'0';
}
else {
int b=sat[cnt];cnt--;
int a=sat[cnt];
if(str[i]=='+')sat[cnt]=a+b;
else if(str[i]=='-')sat[cnt]=a-b;
else if(str[i]=='*')sat[cnt]=a*b;
else sat[cnt]=a/b;
}
}
cout<<sat[1];
return 0;
}
4.队列安排。
思路:先建三个点0,1,maxx(我取得114514)
保证询问得数都能被查到前后。。
代码:
#include<bits/stdc++.h>
using namespace std;
int pre[114515];
int nex[114515];
int shou=0;
int wei=114514;
int N,M;
bool book[114515];
void p(){
int js=nex[0];
cout<<endl;
while(js)cout<<js<<" ",js=nex[js];
cout<<endl;
}
int main(){
nex[0]=1;
nex[1]=114514;
pre[114514]=1;
pre[1]=0;
book[0]=1;
book[114514]=1;
int js=nex[0];
cin>>N;
for(int i=2;i<=N;i++){
int a,b;
cin>>a>>b;
if(b==0){
int wei=pre[a];
nex[wei]=i;nex[i]=a;
pre[a]=i;pre[i]=wei;//wei i a
}
else{
int wei=nex[a];
pre[wei]=i;pre[i]=a;
nex[i]=wei;nex[a]=i;//a i wei
}
//p();
}
cin>>M;
for(int i=1;i<=M;i++){
int op;
cin>>op;
book[op]=1;
}
js=nex[0];
while(js){
if(book[js]==0)cout<<js<<" ";
js=nex[js];
}
return 0;
}
5.中缀改后缀
先上代码:
#include<bits/stdc++.h>
using namespace std;
int you(char ch){
if(ch=='(' || ch==')')return 0;
else if(ch=='+' || ch=='-')return 1;
else if(ch=='*' || ch=='/')return 2;
else return 3;
}
struct cjc{
int id;
int shu;
};
stack<char> EVA_c;
char CH[100000];
int cnt=0;
int main(){
string str;
cin>>str;
int len=str.length();
for(int i=0;i<len;i++){
if(str[i]>='0' && str[i]<='9')CH[++cnt]=str[i];
else{
if(str[i]=='(')EVA_c.push('(');
else if(str[i]==')'){
while(EVA_c.top()!='('){
char ch=EVA_c.top();
CH[++cnt]=ch;
EVA_c.pop();
}
EVA_c.pop();
}
else if(str[i]=='^'){
EVA_c.push(str[i]);
}
else{
int Y=you(str[i]);
while(!EVA_c.empty() && you(EVA_c.top())>=Y && EVA_c.top()!='('){
char ch=EVA_c.top();
CH[++cnt]=ch;
EVA_c.pop();
}
EVA_c.push(str[i]);
}
}
}
while(!EVA_c.empty()){
char ch=EVA_c.top();
EVA_c.pop();
CH[++cnt]=ch;
}
bool book[100000];
for(int i=1;i<=cnt;i++){
cout<<CH[i]<<" ";
}
cout<<endl;
int shu[100000],ans=0;
for(int i=1;i<=cnt;i++){
if(CH[i]>='0' && CH[i]<='9')shu[i]=(int)(CH[i]-'0'),ans++;
else if(CH[i]=='+')shu[i]=1145141;
else if(CH[i]=='-')shu[i]=1145142;
else if(CH[i]=='*')shu[i]=1145143;
else if(CH[i]=='/')shu[i]=1145144;
else shu[i]=1145145;
}
bool flag=0;
ans--;
stack<int>EVA;
while(ans--){
flag=0;
for(int i=1;i<=cnt;i++){
if(flag==1){
EVA.push(shu[i]);
}
else{
if(shu[i]==1145141){
int a,b,c;
a=EVA.top();EVA.pop();
b=EVA.top();EVA.pop();
c=a+b;
EVA.push(c);
flag=1;
}
else if(shu[i]==1145142){
int a,b,c;
b=EVA.top();EVA.pop();
a=EVA.top();EVA.pop();
c=a-b;
EVA.push(c);
flag=1;
}
else if(shu[i]==1145143){
int a,b,c;
b=EVA.top();EVA.pop();
a=EVA.top();EVA.pop();
c=a*b;
EVA.push(c);
flag=1;
}
else if(shu[i]==1145144){
int a,b,c;
b=EVA.top();EVA.pop();
a=EVA.top();EVA.pop();
c=a/b;
EVA.push(c);
flag=1;
}
else if(shu[i]==1145145){
int a,b,c;
b=EVA.top();EVA.pop();
a=EVA.top();EVA.pop();
c=pow(a,b);
EVA.push(c);
flag=1;
}
else{
EVA.push(shu[i]);
}
}
}
cnt-=2;
for(int i=cnt;i>=1;i--){
shu[i]=EVA.top();
EVA.pop();
}
for(int i=1;i<=cnt;i++){
if(shu[i]==1145141)cout<<"+ ";
else if(shu[i]==1145142)cout<<"- ";
else if(shu[i]==1145143)cout<<"* ";
else if(shu[i]==1145144)cout<<"/ ";
else if(shu[i]==1145145)cout<<"^ ";
else cout<<shu[i]<<" ";
}
cout<<endl;
}
return 0;
}
思路:中缀改后缀最重要的思路是:
优先级高的符号不能出现在优先级低的前面。。考虑用单调栈来维护符号。。