zoukankan      html  css  js  c++  java
  • Codeforces 825D Suitable Replacement

    You are given two strings s and t consisting of small Latin letters, string s can also contain '?' characters.

    Suitability of string s is calculated by following metric:

    Any two letters can be swapped positions, these operations can be performed arbitrary number of times over any pair of positions. Among all resulting strings s, you choose the one with the largest number of non-intersecting occurrences of string t. Suitability is this number of occurrences.

    You should replace all '?' characters with small Latin letters in such a way that the suitability of string s is maximal.

    Input

    The first line contains string s (1 ≤ |s| ≤ 106).

    The second line contains string t (1 ≤ |t| ≤ 106).

    Output

    Print string s with '?' replaced with small Latin letters in such a way that suitability of that string is maximal.

    If there are multiple strings with maximal suitability then print any of them.

    Examples
    Input
    ?aa?
    ab
    Output
    baab
    Input
    ??b?
    za
    Output
    azbz
    Input
    abcd
    abacaba
    Output
    abcd
    Note

    In the first example string "baab" can be transformed to "abab" with swaps, this one has suitability of 2. That means that string "baab" also has suitability of 2.

    In the second example maximal suitability you can achieve is 1 and there are several dozens of such strings, "azbz" is just one of them.

    In the third example there are no '?' characters and the suitability of the string is 0.


      题目大意 给定串s和串t,s中包含小写字符和通配符'?'(可以代替一个小写字母),t中只包含小写字母,你可以多次交换s中任意两个字母,使得t在s中出现的次数尽可能多。输出一组最优解的s。

      看到了多次我还以为读错题了(我高估了这道题的难度)。

      因为和顺序无关,那么有关的就只有各个字母出现的数量。

      所以二分t在s中不想交出现的次数(有相交不好判断),判断是显然的。

      注意check的时候计数要用long long,否则会炸int。

    Code

     1 /**
     2  * Codeforces
     3  * Problem#825D
     4  * Accepted
     5  * Time: 31ms
     6  * Memory: 4008k
     7  */
     8 #include <bits/stdc++.h>
     9 using namespace std;
    10 typedef bool boolean;
    11 
    12 const int limit = 1e6;
    13 char s[limit + 5], t[limit + 5];
    14 
    15 inline void init() {
    16     gets(s);
    17     gets(t);
    18 } 
    19 
    20 int ca = 0;
    21 int cs[27], ct[27];
    22 boolean check(int mid) {
    23     long long c = 0;
    24     for(int i = 0; i < 26 && c <= ca; i++)
    25         c += (cs[i] >= ct[i] * 1LL *  mid) ? (0) : (ct[i] * 1LL * mid - cs[i]);
    26     return c <= ca;
    27 }
    28 
    29 inline void solve() {
    30     memset(cs, 0, sizeof(cs));
    31     memset(ct, 0, sizeof(ct));
    32     for(int i = 0; s[i]; i++)
    33         (s[i] >= 'a' && s[i] <= 'z') ? (cs[s[i] - 'a']++) : (ca++);
    34     if(!ca) {
    35         puts(s);
    36         return;
    37     }
    38     for(int i = 0; t[i]; i++)
    39         ct[t[i] - 'a']++;
    40     int l = 1, r = 1e6;
    41     while(l <= r) {
    42         int mid = (l + r) >> 1;
    43         if(check(mid))    l = mid + 1;
    44         else r = mid - 1;
    45     }
    46     int ans = l - 1;
    47     for(int i = 0; i < 26; i++)
    48         ct[i] = max(ct[i] * ans - cs[i], 0);
    49     int p = 0;
    50     for(int i = 0; s[i]; i++) {
    51         while(p < 25 && ct[p] == 0)    p++;
    52         if(s[i] == '?')
    53             s[i] = p + 'a', ct[p]--;
    54     }
    55     puts(s);
    56 }
    57 
    58 int main() {
    59     init();
    60     solve();
    61     return 0;
    62 }
  • 相关阅读:
    20155309 《Java程序设计》实验三(Java面向对象程序设计)实验报告
    2016-2017-2 20155309南皓芯《java程序设计》第十周学习总结
    2015309南皓芯实验二 Java面向对象程序设计
    2016-2017-2 20155309南皓芯《java程序设计》第九周学习总结
    2016-2017-2 20155309 南皓芯《java程序设计》第八周学习总结
    2015309南皓芯《Java程序设计》实验一(Java开发环境的熟悉)实验报告
    2016-2017-2 20155309南皓芯《java程序设计》第七周学习总结
    2016-2017-2 20155309南皓芯java第五周学习总结
    20155304 2016-2017-2 《Java程序设计》第六周学习总结
    20155304 2016-2017-2 《Java程序设计》第五周学习总结
  • 原文地址:https://www.cnblogs.com/yyf0309/p/7295439.html
Copyright © 2011-2022 走看看