为了能到远方,脚下的每一步都不能少.|

Zac-saodiseng

园龄:2年4个月粉丝:3关注:0

ACM预备队-week3(线性表)

1.寄存柜

题目链接:P3613 【深基15.例2】寄包柜 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

二维map

创建一个二维map,这里以string为例

 

1 map<string, map<string, string>> b;

 

增加元素

增加元素可分为两种情况:(1) 增加若干个数据时可以使用 map 中的 insert() 方法,或者 insert_or_assign() 方法。两者的区别:使用insert()方法时,若相应key不存在,则完成插入操作。反之,则不做插入操作。使用 insert_or_assign() 方法时,若相应 key 不存在,则完成插入操作。反之,完成赋值操作。

(2) 增加单独的数据可以使用赋值方法 test[ "维度1" ][ "维度2" ] = " 字符串 ";,这种方法既简单又方便。若原容器中没有相应的 key ,则完成插入操作,反之完成赋值操作。

三维 map 的操作和二维 map 的操作相同,只是多了一个维度

 

复制代码
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 map<int,map<int,int>>a;
 4 int main()
 5 {
 6     int n,q;
 7     int x,i,j,k;
 8     scanf("%d%d",&n,&q);
 9     while(q--)
10     {
11         scanf("%d%d%d",&x,&i,&j);
12         if(x==1)
13         {
14             scanf("%d",&k);
15             a[i][j]=k;//在第i个柜子里第j个格子里存放k值
16         }
17         else
18         {
19             printf("%d\n",a[i][j]);
20         }
21     }
22     return 0;
23 }
复制代码

2.括号序列

题目链接:P1241 括号序列 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

ps:建议把题目理解难度标成黑题qwq,,题目翻译的好lj,

其实个人觉得这样写更好理解,确定一个右括号之后,可以从这个括号之前的一个位置遍历字符串,往前面找有没有对应的左括号,如果找到了一个不对应的左括号,则不匹配,

同理,如果没找到对应类型的左括号,也不匹配

复制代码
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int a[110];
 4 int main()
 5 {
 6     string s;
 7     cin>>s;
 8     int i,j;
 9     for(i=0;i<s.length();i++)
10     {
11         if(s[i]==')')
12         {
13             for(j=i-1;j>=0;j--)
14             {
15                 if(s[j]=='('&&a[j]==0)
16                 {
17                     a[i]=a[j]=1;//标记
18                     break;
19                 }
20                 else if(s[j]=='['&&a[j]==0)break;//匹配失败
21             }
22         }
23         else if(s[i]==']')
24         {
25             for(j=i-1;j>=0;j--)
26             {
27                 if(s[j]=='['&&a[j]==0)
28                 {
29                     a[i]=a[j]=1;
30                     break;
31                 }
32                 else if(s[j]=='('&&a[j]==0)break;//匹配失败
33             }
34         }
35     }
36     for(i=0;i<s.length();i++)
37     {
38         if(a[i]==0)
39         {
40             if(s[i]=='('||s[i]==')')cout<<"()";
41             else cout<<"[]";
42         }
43         else cout<<s[i];
44     }
45     return 0;
46 }
复制代码

3.后缀表达式(逆波兰表达式)

题目链接:P1449 后缀表达式 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

不会有人做后缀表达式的时候还不知道要用栈叭QWQ

复制代码
 1 //模拟栈
 2 #include <bits/stdc++.h>
 3 using namespace std;
 4 stack<int>s;
 5 int x,y,n;
 6 char c;
 7 int main()
 8 {
 9     while( cin>>c &&c!='@')
10     {
11         switch(c)
12         {
13             case '+':x=s.top();s.pop();y=s.top();s.pop();s.push(x+y);break;
14             case '-':x=s.top();s.pop();y=s.top();s.pop();s.push(y-x);break;
15             case '*':x=s.top();s.pop();y=s.top();s.pop();s.push(x*y);break;
16             case '/':x=s.top();s.pop();y=s.top();s.pop();s.push(y/x);break;
17             case '.':s.push(n);n=0;break;
18             default :n=n*10+c-'0';break;
19         }
20     }
21     cout<<s.top();
22     return 0;
23 }
复制代码

4.队列安排(双向链表)

题目链接:P1160 队列安排 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

用l和r数组模拟双向链表,speed++

复制代码
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N=1e5+10;
 4 int l[N],r[N];
 5 int n,m;
 6 void init()
 7 {
 8     r[0]=1;
 9     l[1]=0;
10     r[1]=-1;
11 }
12 
13 int main()
14 {
15     cin>>n;
16     init();
17     for(int i=2;i<=n;i++)
18     {
19         int k,p;
20         cin>>k>>p;
21         if(p==1)
22         {
23             l[i]=k;
24             l[r[k]]=i;
25             r[i]=r[k];
26             r[k]=i;
27         }
28         else
29         {
30             r[i]=k;
31             r[l[k]]=i;
32             l[i]=l[k];
33             l[k]=i;
34         }
35     }
36     //开始remove
37     cin>>m;
38     for(int i=1;i<=m;i++)
39     {
40         int x;
41         cin>>x;
42         if(r[x]==-1&&l[x]==-1)continue;
43         r[l[x]]=r[x];
44         l[r[x]]=l[x];
45         r[x]=-1;
46         l[x]=-1;
47     }
48     for(int i=r[0];i!=-1;i=r[i])
49     {
50         cout<<i<<' ';
51     }
52     return 0;
53 }
复制代码

本文作者:Zac-saodiseng

本文链接:https://www.cnblogs.com/Zac-saodiseng/p/16888266.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Zac-saodiseng  阅读(1559)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起