zoukankan      html  css  js  c++  java
  • AccurateLee双指针+贪心+字符串

    B. AccurateLee
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Lee was cleaning his house for the party when he found a messy string under the carpets. Now he'd like to make it clean accurately and in a stylish way...

    The string ss he found is a binary string of length nn (i. e. string consists only of 0-s and 1-s).

    In one move he can choose two consecutive characters sisi and si+1si+1, and if sisi is 1 and si+1si+1 is 0, he can erase exactly one of them (he can choose which one to erase but he can't erase both characters simultaneously). The string shrinks after erasing.

    Lee can make an arbitrary number of moves (possibly zero) and he'd like to make the string ss as clean as possible. He thinks for two different strings xx and yy, the shorter string is cleaner, and if they are the same length, then the lexicographically smaller string is cleaner.

    Now you should answer tt test cases: for the ii-th test case, print the cleanest possible string that Lee can get by doing some number of moves.

    Small reminder: if we have two strings xx and yy of the same length then xx is lexicographically smaller than yy if there is a position ii such that x1=y1x1=y1, x2=y2x2=y2,..., xi1=yi1xi−1=yi−1 and xi<yixi<yi.

    Input

    The first line contains the integer tt (1t1041≤t≤104) — the number of test cases.

    Next 2t2t lines contain test cases — one per two lines.

    The first line of each test case contains the integer nn (1n1051≤n≤105) — the length of the string ss.

    The second line contains the binary string ss. The string ss is a string of length nn which consists only of zeroes and ones.

    It's guaranteed that sum of nn over test cases doesn't exceed 105105.

    Output

    Print tt answers — one per test case.

    The answer to the ii-th test case is the cleanest string Lee can get after doing some number of moves (possibly zero).

    Example
    input
    Copy
    5
    10
    0001111111
    4
    0101
    8
    11001101
    10
    1110000000
    1
    1
    
    output
    Copy
    0001111111
    001
    01
    0
    1
    
    Note

    In the first test case, Lee can't perform any moves.

    In the second test case, Lee should erase s2s2.

    In the third test case, Lee can make moves, for example, in the following order: 11001101 → 1100101 → 110101 → 10101 → 1101 → 101 → 01.

    觉得比赛的代码巨lj:

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const ll inf = 0x3f3f3f3f;
    const ll mod = 1e9+7;
    const double eps = 1e-8;
    const ll mx = 1e6+10; //check the limits, dummy
    typedef pair<int, int> pa;
    const double PI = acos(-1);
    ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
    ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
    bool isprime(int n) { if (n <= 1)return 0; for (int i = 2; i * i <= n; i++)if (n % i == 0)return 0; return 1; }
    #define swa(a,b) a^=b^=a^=b
    #define re(i,a,b) for(ll i=(a),_=(b);i<_;i++)
    #define rb(i,a,b) for(ll i=(a),_=(b);i>=_;i--)
    #define clr(a,b) memset(a, b, sizeof(a))
    #define lowbit(x) ((x)&(x-1))
    #define mkp make_pair
    inline ll qpow(ll a, ll b) { return b ? ((b & 1) ? a * qpow(a * a % mod, b >> 1) % mod : qpow(a * a % mod, b >> 1)) % mod : 1; }
    inline ll qpow(ll a, ll b, ll c) { return b ? ((b & 1) ? a * qpow(a * a % c, b >> 1) % c : qpow(a * a % c, b >> 1)) % c : 1; }
    void ca(int kase, int ans) { cout << "Case #" << kase << ": " << ans << endl; }
    //void sc(int& x) { scanf("%d", &x); }void sc(int64_t& x) { scanf("%lld", &x); }void sc(double& x) { scanf("%lf", &x); }void sc(char& x) { scanf(" %c", &x); }void sc(char* x) { scanf("%s", x); }
    ll t,m,n,k,l,r;
    ll sum = 0;
    
    //ll a[mx],b[mx];
    ll cnt = 0;
    int main()
    {
        ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
        cin >> t;
        while (t--) {
            string s;
            cin >> n;
            cin >> s;
            //ll i = 0, j = n - 1;
            ll a=-inf, b=inf;
            re(i,0,n){
                if (s[i] == '1') {
                    a = i; break;
                }
            }
            for (ll i = n - 1; i >= 0; i--) {
                if (s[i] == '0') {
                    b = i; break;
                }
            }
            if (a > b||a==-inf||b==inf)cout<< s;
            else {
                re(i, 0, a)cout<< s[i];
                cout<< 0;
                re(i, b+1, n)cout<< s[i];
            }
            cout << endl;
        }
        return 0;
    }

    优化一下:

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const ll inf = 0x3f3f3f3f;
    const ll mod = 1e9+7;
    const double eps = 1e-8;
    const ll mx = 1e6+10; //check the limits, dummy
    typedef pair<int, int> pa;
    const double PI = acos(-1);
    ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
    ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
    bool isprime(int n) { if (n <= 1)return 0; for (int i = 2; i * i <= n; i++)if (n % i == 0)return 0; return 1; }
    #define swa(a,b) a^=b^=a^=b
    #define re(i,a,b) for(ll i=(a),_=(b);i<_;i++)
    #define rb(i,a,b) for(ll i=(a),_=(b);i>=_;i--)
    #define clr(a,b) memset(a, b, sizeof(a))
    #define lowbit(x) ((x)&(x-1))
    #define mkp make_pair
    inline ll qpow(ll a, ll b) { return b ? ((b & 1) ? a * qpow(a * a % mod, b >> 1) % mod : qpow(a * a % mod, b >> 1)) % mod : 1; }
    inline ll qpow(ll a, ll b, ll c) { return b ? ((b & 1) ? a * qpow(a * a % c, b >> 1) % c : qpow(a * a % c, b >> 1)) % c : 1; }
    void ca(int kase, int ans) { cout << "Case #" << kase << ": " << ans << endl; }
    //void sc(int& x) { scanf("%d", &x); }void sc(int64_t& x) { scanf("%lld", &x); }void sc(double& x) { scanf("%lf", &x); }void sc(char& x) { scanf(" %c", &x); }void sc(char* x) { scanf("%s", x); }
    ll t,m,n,k,l,r;
    ll sum = 0;
    //ll a[mx],b[mx];
    ll cnt = 0;
    int main()
    {
        ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
        cin >> t;
        while (t--) {
            cin >> n;
            string s;
            cin >> s;
            int i = 0, j = n;
            while (i < n && s[i] == '0')++i;
            while (j > 0 && s[j - 1] == '1')--j;
            if (i == j)  cout<< s << "
    ";
            else cout << s.substr(0, i) + '0' + s.substr(j) << "
    ";
        }
        return 0;
    }
  • 相关阅读:
    IBatisNet之获取和操作SQL语句
    IIS7站点/虚拟目录中访问共享文件夹(转)
    asp.net 4.0 IIS7.0/7.5环境提供了自动预热功能(程序池自动重启)
    仅此一文让你明白ASP.NET MVC原理
    博客、论坛集
    软件开发工具
    delete表1条件是另一个表中的数据,多表连接删除(转)
    Quartz.Net架构入门—开源的作业调度框架
    C# URL 中文编码与解码
    HDU4619--Warm up 2
  • 原文地址:https://www.cnblogs.com/xxxsans/p/13185566.html
Copyright © 2011-2022 走看看