P1160
| #include <cstdio> |
| const int N = 1e5 + 5; |
| int left[N], right[N]; |
| bool del[N]; |
| int main() |
| { |
| int n; scanf("%d", &n); |
| right[0]=1; left[1]=0; |
| right[1]=n+1; left[n+1]=1; |
| for (int i=2;i<=n;i++) { |
| int k,p; scanf("%d%d", &k,&p); |
| if (p==0) { |
| |
| int l=left[k]; |
| |
| right[l]=i; left[i]=l; |
| right[i]=k; left[k]=i; |
| } else { |
| |
| int r=right[k]; |
| |
| right[k]=i; left[i]=k; |
| right[i]=r; left[r]=i; |
| } |
| } |
| |
| int m; scanf("%d", &m); |
| for (int i=1;i<=m;i++) { |
| int x; scanf("%d",&x); |
| if (!del[x]) { |
| int l=left[x],r=right[x]; |
| |
| right[l]=r; left[r]=l; |
| del[x]=true; |
| } |
| } |
| |
| int x=right[0]; |
| |
| while (x!=n+1) { |
| printf("%d ", x); |
| x=right[x]; |
| } |
| return 0; |
| } |
| |
栈
在超市购物的时候,超市入口处往往会放一排购物车。这一排购物车一个插在另一个的后面,顺序排列,第一个一般靠着墙,不好拿。这时候,如果想取走一辆,一般是先取走最后面的一辆,下一个来购物的顾客取走倒数第二辆。与之对应的,如果顾客用完了购物车,要把购物车还回去,那么归还的这辆购物车通常也是放在最后面。在通常情况下,最后面的车会被反复取用,而第一辆很少被用到,除非其他购物车都被取走了。

平常写表达式,一般运算符在数的中间,比如 ,其中 在 和 之间, 在 和 中间,这种表达式称为中缀表达式。中缀表达式对人类友好,但对计算机没那么友好。对计算机友好的表达式是“后缀表达式”,顾名思义,后缀表达式中运算符在参数的后面。对于计算机而言,后缀表达式比中缀表达式更容易计算,另外,后缀表达式的运算机制可以避免掉括号,这也是它相对于中缀表达式的一大优势。

计算题:中缀表达式 ((6-3)*2+7)/(5^(3*4+2))
对应的后缀表达式为?
答案

| #include <cstdio> |
| #include <cstring> |
| #include <stack> |
| using std::stack; |
| const int N = 55; |
| char s[N]; |
| int main() |
| { |
| scanf("%s", s + 1); |
| int len = strlen(s + 1); |
| int num=0; |
| stack<int> st; |
| for (int i = 1; i <= len; i++) { |
| if (s[i]>='0'&&s[i]<='9') { |
| |
| num=num*10+(s[i]-'0'); |
| } else if (s[i]=='.') { |
| |
| st.push(num); |
| num=0; |
| } else if (s[i]=='@') { |
| break; |
| } else { |
| |
| |
| |
| |
| |
| |
| int x=st.top(); st.pop(); |
| int y=st.top(); st.pop(); |
| |
| int z; |
| if (s[i]=='+') { |
| z=y+x; |
| } else if (s[i]=='-') { |
| z=y-x; |
| } else if (s[i]=='*') { |
| z=y*x; |
| } else { |
| z=y/x; |
| } |
| st.push(z); |
| } |
| } |
| printf("%d\n", st.top()); |
| return 0; |
| } |
| |
P4387
| #include <cstdio> |
| #include <stack> |
| using std::stack; |
| const int N = 1e5 + 5; |
| int in[N], out[N]; |
| int main() |
| { |
| int q; scanf("%d", &q); |
| for (int i = 1; i <= q; i++) { |
| int n; scanf("%d", &n); |
| for (int j = 1; j <= n; j++) scanf("%d", &in[j]); |
| for (int j = 1; j <= n; j++) scanf("%d", &out[j]); |
| int kout=1; |
| int kin=1; |
| stack<int> s; |
| while (true) { |
| if (!s.empty() && kout<=n && s.top()==out[kout]) { |
| s.pop(); kout++; |
| } else if (kin<=n) { |
| s.push(in[kin]); kin++; |
| } else { |
| break; |
| } |
| } |
| if (kout==n+1) printf("Yes\n"); |
| else printf("No\n"); |
| } |
| return 0; |
| } |
| |
P2201
| #include <cstdio> |
| #include <stack> |
| #include <algorithm> |
| using std::stack; |
| using std::max; |
| const int N = 1e6 + 5; |
| struct Data { |
| int x,s,maxs; |
| }; |
| Data a[N]; |
| int top; |
| char op[5]; |
| int main() |
| { |
| a[0].s=0; a[0].maxs=-2000; |
| int n; scanf("%d",&n); |
| stack<int> s2; |
| for (int i=1;i<=n;i++) { |
| scanf("%s",op); |
| char ch=op[0]; |
| if (ch=='I') { |
| int x; scanf("%d",&x); |
| |
| |
| top++; |
| a[top].x = x; |
| a[top].s = a[top-1].s+x; |
| a[top].maxs = max(a[top-1].maxs, a[top].s); |
| } else if (ch=='D') { |
| top--; |
| } else if (ch=='L') { |
| s2.push(a[top].x); top--; |
| } else if (ch=='R') { |
| int x=s2.top(); |
| top++; |
| a[top].x = x; |
| a[top].s = a[top-1].s+x; |
| a[top].maxs = max(a[top-1].maxs, a[top].s); |
| s2.pop(); |
| } else { |
| int k; scanf("%d",&k); |
| printf("%d\n", a[k].maxs); |
| } |
| } |
| return 0; |
| } |
| |
队列

选择题:下述代码实现的数据结构是
| int data[100], f = 1, r; |
| void insert(int value) { |
| data[++r] = value; |
| } |
| void pop() { |
| f++; |
| } |
A. 链表
B. 栈
C. 队列
D. 平衡树
答案
C
P1540
| #include <cstdio> |
| #include <queue> |
| using std::queue; |
| const int N = 1005; |
| bool inq[N]; |
| int main() |
| { |
| int m, n; scanf("%d%d",&m,&n); |
| queue<int> q; |
| int cnt=0; |
| int ans=0; |
| for (int i=1;i<=n;i++) { |
| int x; scanf("%d",&x); |
| if (!inq[x]) { |
| if (cnt>=m) { |
| inq[q.front()]=false; |
| cnt--; |
| q.pop(); |
| } |
| q.push(x); inq[x]=true; cnt++; |
| ans++; |
| } |
| } |
| printf("%d\n",ans); |
| return 0; |
| } |
| |
P2058
| #include <cstdio> |
| #include <queue> |
| using std::queue; |
| const int N = 1e5 + 5; |
| struct Person { |
| int t, id; |
| }; |
| int cnt[N]; |
| int main() |
| { |
| queue<Person> q; |
| int n; scanf("%d",&n); |
| int ans=0; |
| for (int i=1;i<=n;i++) { |
| int t,k; scanf("%d%d",&t,&k); |
| |
| for (int j=1;j<=k;j++) { |
| int x; scanf("%d",&x); |
| q.push({t,x}); cnt[x]++; |
| if (cnt[x]==1) { |
| ans++; |
| } |
| } |
| |
| while (q.front().t <= t-86400) { |
| int x=q.front().id; |
| cnt[x]--; |
| if (cnt[x]==0) { |
| ans--; |
| } |
| q.pop(); |
| } |
| printf("%d\n",ans); |
| } |
| return 0; |
| } |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?