zoukankan      html  css  js  c++  java
  • [CF797C] Minimal string(贪心,栈)

    题目链接:http://codeforces.com/contest/797/problem/C

    题意:给个字符串,求字典序最小的出栈顺序。

    一开始想用优先队列记录全局最小的字符,然后每次入栈的时候检查当前字符是不是最小的,如果是那么同时pop。这样做的话,假如不是,那么栈里面的最小就找不到了。

    所以重写,直接维护一个数组sm[i]表示i之后的字符最小的是谁。从头扫,每次都入栈。将栈里小于等于当前位置后缀的最小字符pop输出就行了。

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 typedef long long LL;
     5 const int maxn = 100100;
     6 const int inf = 'z'+1;
     7 int n;
     8 char s[maxn], t[maxn], sm[maxn];
     9 priority_queue<char> pq;
    10 
    11 int main() {
    12     // freopen("in", "r", stdin);
    13     while(~scanf("%s", &s)) {
    14         n = strlen(s);
    15         sm[n] = inf;
    16         for(int i = n - 1; i >= 0; i--) {
    17             sm[i] = min(sm[i+1], s[i]);
    18         }
    19         int j = 0;
    20         for(int i = 0; i < n; i++) {
    21             t[j++] = s[i];
    22             while(j && t[j-1] <= sm[i+1]) {
    23                 printf("%c", t[--j]);
    24             }
    25         }
    26         printf("
    ");
    27     }
    28     return 0;
    29 }
  • 相关阅读:
    MySQL Cluster --01
    TPCC-MySQL(转自imysql.com)
    MySQL mha 高可用集群搭建
    Redis--初入
    Python之Fabric
    MySQL pt-table-checksum及pt-table-sync校验及修复主从一致性
    MySQL GTID
    MySQL Batched Key Access
    MySQL Block Nested-Loop Join(BNL)
    MySQL Index Condition Pushdown
  • 原文地址:https://www.cnblogs.com/kirai/p/6846027.html
Copyright © 2011-2022 走看看