Codeforces 777D Cloud of Hashtags(贪心)

题目链接 Cloud of Hashtags

题目还是比较简单的,直接贪心,但是因为我有两个细节没注意,所以FST了:

1、用了cin读入,但是没有加 std::ios::sync_with_stdio(false); 这条语句;

2、开了太多string。

也算是经验教训吧。

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)              for(int i(a); i <= (b); ++i)
#define dec(i, a, b)              for(int i(a); i >= (b); --i)

const int N     =    500000      +       10;
int n;
string s[N], c[N];

int main(){

    std::ios::sync_with_stdio(false);
    cin >> n;
    rep(i, 1, n) cin >> s[i];
    c[n] = s[n];
    dec(i, n - 1, 1){
        int j = i + 1;
        if (s[i] <= c[j]){
            c[i] = s[i];
            continue;
        }
        int l1 = s[i].length(), l2 = c[j].length();
        int p;
        rep(k, 1, l1 - 1){
            if (s[i][k] > c[j][k] || k > l2 - 1){
                p = k - 1;
                break;
            }
        }

        c[i] = "";
        rep(k, 0, p) c[i] = c[i] + s[i][k]; 
    }

    rep(i, 1, n) cout << c[i] << endl;


    return 0;

}
原文地址:https://www.cnblogs.com/cxhscst2/p/6443685.html