zoukankan      html  css  js  c++  java
  • 51nod1255(栈)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1255

    题意:中文题诶~

    思路:对于当前字符 s[i],若其不在栈中,将其与栈顶元素比较,若 s.top() > s[i],则退栈至s.top() < s[i] 或者 i 后面不存在字符 s.top(),再将 s[i] 入栈;

    先预处理一个 tag[i][j] 子串 s[i....n-1]有多少字符 j ;

    代码:

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 using namespace std;
     5 
     6 const int MAXN = 1e5+10;
     7 int tag[MAXN][26], vis[26], indx = 0;
     8 char q[30];
     9 
    10 int main(void){
    11     string s;
    12     cin >> s;
    13     for(int i = s.size() - 1; i >= 0; i--){
    14         for(int j = 0; j < 26; j++){
    15             if(j == s[i] - 'a') tag[i][j] = tag[i + 1][j] + 1;
    16             else tag[i][j] = tag[i + 1][j];
    17         }
    18     }
    19     for(int i = 0; i < s.size(); i++){
    20         if(!vis[s[i] - 'a']){
    21             while(indx > 0){
    22                 int cnt = q[indx] - 'a';
    23                 if(cnt < s[i] - 'a' || tag[i + 1][cnt] == 0) break;
    24                 --indx;
    25                 vis[cnt] = 0;
    26             }
    27             q[++indx] = s[i];
    28             vis[s[i] - 'a'] = 1;
    29         }
    30     }
    31     for(int i = 1; i <= indx; i++){
    32         printf("%c", q[i]);
    33     }
    34     puts("");
    35     return 0;
    36 }
    View Code
  • 相关阅读:
    UVA 12284 Digital Matrix
    lightoj 1052
    light oj 1236
    light oj 1151
    省选准备 MISTAKE 大全
    我的省选 Day -15
    「FJ2014集训」采药人的路径
    【NOI2012】迷失游乐园
    寒假这十天
    计算几何 大杂烩
  • 原文地址:https://www.cnblogs.com/geloutingyu/p/6945840.html
Copyright © 2011-2022 走看看