【SRM 717 div2 B】LexmaxReplace

Problem Statement

Alice has a string s of lowercase letters. The string is written on a wall.
Alice also has a set of cards. Each card contains a single letter. Alice can take any card and glue it on top of one of the letters of s. She may use any subset of cards in this way, possibly none or all of them. She is not allowed to glue new letters in front of s or after s, she can only replace the existing letters.
Alice wants to produce the lexicographically largest possible string.
You are given the string s. You are also given a string t. Each character of t is a letter written on one of the cards. Compute and return the lexicographically largest string Alice can produce on the wall while following the rules described above.
Definition

Class:
LexmaxReplace
Method:
get
Parameters:
string, string
Returns:
string
Method signature:
string get(string s, string t)
(be sure your method is public)
Limits

Time limit (s):
2.000
Memory limit (MB):
512
Stack limit (MB):
512

Notes

Given two distinct strings X and Y of the same length, the lexicographically larger one is the one that has a larger character on the first position on which they differ.

Constraints

s will contain between 1 and 50 characters, inclusive.

t will contain between 1 and 50 characters, inclusive.

s will contain only lowercase English letters (‘a’ - ‘z’).

t will contain only lowercase English letters (‘a’ - ‘z’).
Examples
0)

“abb”
“c”
Returns: “cbb”
Alice has a single card. This card contains the letter ‘c’. The optimal solution is to glue it on top of s[0], producing the string “cbb”.
1)

“z”
“f”
Returns: “z”
Here the optimal solution is to do nothing. The card with the letter ‘f’ will remain unused.
2)

“fedcba”
“ee”
Returns: “feeeba”

3)

“top”
“coder”
Returns: “trp”

4)

“xldyzmsrrwzwaofkcxwehgvtrsximxgdqrhjthkgfucrjdvwlr”
“xfpidmmilhdfzypbguentqcojivertdhshstkcysydgcwuwhlk”
Returns: “zyyyzyxwwwzwvuuttxwtssvtssxrqxppqrontmmllukrkjvwlr”

【题目链接】:

【题意】

你有最多50个字符集合,
每个字符最多只能用一次;
(所有字符只包含小写字母)
然后给你一个字符串s;
让你使用这个字符集合里面的字符,去代替字符串s的每一位,使得这个字符串s的字典序达到最大;

【题解】

顺序枚举每一位,对于每一位,找一找有没有比这个字母的字典序大的字符,有的话,就用那个代替这一位上面的字母;
没有就跳过;

【Number Of WA

0

【反思】

比较明显的题。

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 110;
//head

map <char,int> dic;

class LexmaxReplace
{
    public:
        string get(string s, string t)
        {
            rep1(i,0,(int) t.size()-1){
                dic[t[i]]++;
            }
            rep1(i,0,(int) s.size()-1){
                for (char key = 'z';key>=s[i]+1;key--)
                    if (dic[key]>0){
                        dic[key]--;
                        s[i] = key;
                        break;
                    }
            }
            return s;
        }
};
原文地址:https://www.cnblogs.com/AWCXV/p/7626229.html