SRM 396(1-250pt)

DIV1 250pt

题意:对于一个字符串s,若对于每一个i = 0 to s.size()-p-1都有s[i] = s[i+p]则称字符串s是p循环的。"CATCATC", "CATCAT", "ACTAC" , "ACT"都是3循环。给定字符串s和一个数字ma,问至少修改多少个s中字符才能使得s变为k循环的,且k <= ma。

解法:水题,暴力枚举k = 1 to ma即可。

tag:think

  1 // BEGIN CUT HERE
  2 /*
  3  * Author:  plum rain
  4  * score :
  5  */
  6 /*
  7 
  8  */
  9 // END CUT HERE
 10 #line 11 "DNAString.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 sz(v) ((int)(v).size())
 41 #define all(t) t.begin(),t.end()
 42 #define zero(x) (((x)>0?(x):-(x))<eps)
 43 #define out(x) cout<<#x<<":"<<(x)<<endl
 44 #define tst(a) cout<<a<<" "
 45 #define tst1(a) cout<<#a<<endl
 46 #define CINBEQUICKER std::ios::sync_with_stdio(false)
 47 
 48 typedef vector<int> VI;
 49 typedef vector<string> VS;
 50 typedef vector<double> VD;
 51 typedef pair<int, int> pii;
 52 typedef long long int64;
 53 
 54 const double eps = 1e-8;
 55 const double PI = atan(1.0)*4;
 56 const int inf = 2139062143 / 2;
 57 
 58 string s;
 59 int d[10];
 60 string temp = "ACTG";
 61 
 62 int gao(int x)
 63 {
 64     int ret = 0;
 65     for (int i = 0; i < x; ++ i){
 66         clr0 (d);
 67         int j = i;
 68         while (j < sz(s)){
 69             for (int t = 0; t < 4; ++ t)
 70                 if (temp[t] == s[j]) ++ d[t];
 71             j += x;
 72         }
 73         int ma = 0, sum = 0;
 74         for (int k = 0; k < 4; ++ k)
 75             ma = max(ma, d[k]), sum += d[k];
 76         ret += sum - ma;
 77     }
 78     return ret;
 79 }
 80 
 81 class DNAString
 82 {
 83     public:
 84         int minChanges(int m, vector<string> dna){
 85             s = accumulate(dna.begin(), dna.end(), string(""));
 86             int ans = inf;
 87             for (int i = 1; i <= m; ++ i) ans = min(ans, gao(i));
 88             return ans;
 89         }
 90         
 91 // BEGIN CUT HERE
 92     public:
 93     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(); }
 94     private:
 95     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(); }
 96     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; } }
 97     void test_case_0() { int Arg0 = 3; string Arr1[] = {"ATAGATA"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 1; verify_case(0, Arg2, minChanges(Arg0, Arg1)); }
 98     void test_case_1() { int Arg0 = 2; string Arr1[] = {"ACGTGCA"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 3; verify_case(1, Arg2, minChanges(Arg0, Arg1)); }
 99     void test_case_2() { int Arg0 = 13; string Arr1[] = {"ACGCTGACAGATA"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 0; verify_case(2, Arg2, minChanges(Arg0, Arg1)); }
100     void test_case_3() { int Arg0 = 1; string Arr1[] = {"AAAATTTCCG"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 6; verify_case(3, Arg2, minChanges(Arg0, Arg1)); }
101     void test_case_4() { int Arg0 = 12; string Arr1[] = {"ACGTATAGCATGACA","ACAGATATTATG","ACAGATGTAGCAGTA","ACCA","GAC"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 20; verify_case(4, Arg2, minChanges(Arg0, Arg1)); }
102 
103 // END CUT HERE
104 
105 };
106 
107 // BEGIN CUT HERE
108 int main()
109 {
110 //    freopen( "a.out" , "w" , stdout );    
111     DNAString ___test;
112     ___test.run_test(-1);
113        return 0;
114 }
115 // END CUT HERE
View Code
原文地址:https://www.cnblogs.com/plumrain/p/srm_396.html