zoukankan      html  css  js  c++  java
  • hdu 3183(贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183

    思路:比较前后两个相邻的字符,如果前面一个字符大于后面一个字符,就把它去掉。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <string>
    #include <vector>
    #define REP(i, a, b) for (int i = (a); i < (b); ++i)
    #define FOR(i, a, b) for (int i = (a); i <= (b): ++i)
    using namespace std;
    
    const int MAX_N = (1000 + 100);
    int N, M, len;
    char str[MAX_N], ans[MAX_N];
    
    int main()
    {
    	while (cin >> str >> M) {
    		N = strlen(str), len = 0;
    		REP(i, 0, N - 1) {
    			if (str[i] > str[i + 1] && M) {
    				--M;
    				while (len - 1 >= 0 && ans[len - 1] > str[i + 1] && M) {
    					--len, --M;
    				}
    			}
    			else
    				ans[len++] = str[i];
    		}
    		ans[len++] = str[N - 1];
    		string tmp = "";
    		REP(i, 0, len - M) tmp += ans[i];
    		int p = 0;
    		N = (int)tmp.size(), len = 0;
    		while (p < N && tmp[p] == '0')++p;
    		while (p < N) ans[len++] = tmp[p], ++p;
    		ans[len] = 0;
    		if (strlen(ans) == 0) {
    			cout << 0 << endl;
    		}
    		else
    			cout << ans << endl;
    	}
    	return 0;
    }
    
    


  • 相关阅读:
    IP fragmentation
    pci驱动
    cpu地址空间
    CentOS7 安装bazel
    kettle集群
    TextRankGloVe算法资料
    使用Kong Service负载均衡Load Balance功能
    自建ss服务器教程
    OpenSSL创建TLS/SSL证书
    监控告警
  • 原文地址:https://www.cnblogs.com/wally/p/4477069.html
Copyright © 2011-2022 走看看