zoukankan      html  css  js  c++  java
  • SRM 394(1-250pt)

    DIV1 250pt

    题意:给定一个字符串s('a'-'z'),计其中出现次数最多和最少的字母分别出现c1次和c2次,若在s中去掉最多k个字母,求去掉以后c1 - c2的最小值。

    解法:做题的时候,想到了用dfs暴力枚举,然后TLE了。然后想到了枚举c2,求c1的最小值,最后写了比较麻烦的代码,过了。然后看了题解才发现,枚举c1和c2。。。。。

       真的是看到'a' - 'z'就应该想到这种方法。。。。

    tag:think, brute-force

      1 // BEGIN CUT HERE
      2 /*
      3  * Author:  plum rain
      4  * score :
      5  */
      6 /*
      7 
      8  */
      9 // END CUT HERE
     10 #line 11 "RoughStrings.cpp"
     11 #include <sstream>
     12 #include <stdexcept>
     13 #include <functional>
     14 #include <iomanip>
     15 #include <numeric>
     16 #include <fstream>
     17 #include <cctype>
     18 #include <iostream>
     19 #include <cstdio>
     20 #include <vector>
     21 #include <cstring>
     22 #include <cmath>
     23 #include <algorithm>
     24 #include <cstdlib>
     25 #include <set>
     26 #include <queue>
     27 #include <bitset>
     28 #include <list>
     29 #include <string>
     30 #include <utility>
     31 #include <map>
     32 #include <ctime>
     33 #include <stack>
     34 
     35 using namespace std;
     36 
     37 #define clr0(x) memset(x, 0, sizeof(x))
     38 #define clr1(x) memset(x, -1, sizeof(x))
     39 #define pb push_back
     40 #define mp make_pair
     41 #define sz(v) ((int)(v).size())
     42 #define all(t) t.begin(),t.end()
     43 #define zero(x) (((x)>0?(x):-(x))<eps)
     44 #define out(x) cout<<#x<<":"<<(x)<<endl
     45 #define tst(a) cout<<a<<" "
     46 #define tst1(a) cout<<#a<<endl
     47 #define CINBEQUICKER std::ios::sync_with_stdio(false)
     48 
     49 typedef vector<int> VI;
     50 typedef vector<string> VS;
     51 typedef vector<double> VD;
     52 typedef pair<int, int> pii;
     53 typedef long long int64;
     54 
     55 const double eps = 1e-8;
     56 const double PI = atan(1.0)*4;
     57 const int inf = 2139062143 / 2;
     58 
     59 int num[500], n2[500], n1[500];
     60 int idx[500], all;
     61 pii an[100];
     62 
     63 int gao(int k)
     64 {
     65     clr0 (n2);
     66     for (int i = 0; i < 26; ++ i)
     67         ++ n2[num[i]];
     68     int pos = 50;
     69     while (!n2[pos]) -- pos;
     70     while (k > 0){
     71         if (pos < 0) return 0;
     72         k -= n2[pos];
     73         n2[pos-1] += n2[pos];
     74         n2[pos--] = 0;
     75     }
     76     if (!k) return pos;
     77     return pos + 1;
     78 }
     79 
     80 class RoughStrings
     81 {
     82     public:
     83         int minRoughness(string s, int k){
     84             clr0 (num);
     85             for (int i = 0; i < sz(s); ++ i)
     86                 ++ num[s[i] - 'a'];
     87             all = 0;
     88             for (int i = 0; i < 26; ++ i)
     89                 if (num[i]) n1[all++] = num[i];
     90             if (all == 1) return 0;
     91             sort(n1, n1+all);
     92             int ans = max(gao(k) - n1[0], 0), use = 0;
     93             for (int i = 0; i < all; ++ i){
     94                 use += n1[i];
     95                 if (k < use) break;
     96                 if (i == all-1) ans = 0;
     97                 else ans = min(ans, max(gao(k-use) - n1[i+1], 0));
     98             }
     99             return ans;
    100         }
    101         
    102 // BEGIN CUT HERE
    103     public:
    104     void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); }
    105     private:
    106     template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '"' << *iter << "","; os << " }"; return os.str(); }
    107     void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "	Expected: "" << Expected << '"' << endl; cerr << "	Received: "" << Received << '"' << endl; } }
    108     void test_case_0() { string Arg0 = "aaaaabbc"; int Arg1 = 1; int Arg2 = 3; verify_case(0, Arg2, minRoughness(Arg0, Arg1)); }
    109     void test_case_1() { string Arg0 = "aaaabbbbc"; int Arg1 = 5; int Arg2 = 0; verify_case(1, Arg2, minRoughness(Arg0, Arg1)); }
    110     void test_case_2() { string Arg0 = "veryeviltestcase"; int Arg1 = 1; int Arg2 = 2; verify_case(2, Arg2, minRoughness(Arg0, Arg1)); }
    111     void test_case_3() { string Arg0 = "gggggggooooooodddddddllllllluuuuuuuccckkk"; int Arg1 = 5; int Arg2 = 3; verify_case(3, Arg2, minRoughness(Arg0, Arg1)); }
    112     void test_case_4() { string Arg0 = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"; int Arg1 = 17; int Arg2 = 0; verify_case(4, Arg2, minRoughness(Arg0, Arg1)); }
    113     void test_case_5() { string Arg0 = "bbbccca"; int Arg1 = 2; int Arg2 = 0; verify_case(5, Arg2, minRoughness(Arg0, Arg1)); }
    114 
    115 // END CUT HERE
    116 
    117 };
    118 
    119 // BEGIN CUT HERE
    120 int main()
    121 {
    122 //    freopen( "a.out" , "w" , stdout );    
    123     RoughStrings ___test;
    124     ___test.run_test(-1);
    125        return 0;
    126 }
    127 // END CUT HERE
    View Code

     网上看到的代码:

  • 相关阅读:
    Linux 下的类似Windows下Everything的搜索工具
    windows和linux环境下制作U盘启动盘
    程序调试手段之gdb, vxworks shell
    LeetCode 1021. Remove Outermost Parentheses (删除最外层的括号)
    LeetCode 1047. Remove All Adjacent Duplicates In String (删除字符串中的所有相邻重复项)
    LeetCode 844. Backspace String Compare (比较含退格的字符串)
    LeetCode 860. Lemonade Change (柠檬水找零)
    LeetCode 1221. Split a String in Balanced Strings (分割平衡字符串)
    LeetCode 1046. Last Stone Weight (最后一块石头的重量 )
    LeetCode 746. Min Cost Climbing Stairs (使用最小花费爬楼梯)
  • 原文地址:https://www.cnblogs.com/plumrain/p/srm_394.html
Copyright © 2011-2022 走看看