UESTC 761 LoveZx与期末考试

被卡的一道题,其他情况都想出来了,主要是没想好A[i] == B[j]时候的处理,取最后面最大的可能不是最优解,相等的时候我暴力比较后缀的(为此还要维护一个链),这个操作是O(len) 所以T了。(也可能是写挂了,全是O(len)的数据是很难造的,看过的名单似乎暴力可行)

实际上取后面最大的只是一种可能,用来更新答案就好了,后续地讨论只在保证前面相等进行。

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

const int LEN = 1e5+5, WEIGHT = 10;
char A[LEN];
char B[LEN];
int cnt[WEIGHT];

char calMax()
{
    for(int c = WEIGHT; c--;){
        if(cnt[c]) return c + '0';
    }
    return '0'-1;
}

int solve()
{
    scanf("%s%s",A,B);
    int l1 = strlen(A), l2 = strlen(B);
    if(l2 < l1){
        return LEN;
    }
    if(l2 > l1){
        return 0;
    }

    memset(cnt,0,sizeof(cnt));
    int i, j;
    for(i = 0; i < l2; i++) {
        cnt[B[i]-'0']++;
    }

    int cur_move = 0, ans = LEN;
    for(i = 0, j = 0; i < l1; i++){
        if(A[i] < B[j]) break;
        if(A[i] > B[j]){
            char mx = calMax();
            if(mx < A[i]) {
                cur_move = LEN; break;
            }
            cur_move++;
            if(mx > A[i]) break;
            cnt[mx - '0']--;
        }
        else {
            if(calMax() > A[i]) ans = min(cur_move+1,ans);
            cnt[B[j]-'0']--;
            while(!cnt[B[++j]-'0']);
        }
    }
    if(i < l1) ans = min(ans, cur_move); //equal
    return ans;
}

//#define LOCAL
int main()
{
#ifdef LOCAL
    freopen("in.txt","r",stdin);
#endif
    int T; scanf("%d",&T);
    while(T--){
        int res = solve();
        if(res < LEN) printf("%d
", res);
        else puts("Poor LoveZx");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/jerryRey/p/5003329.html