CodeForce-1196D2-RGB Substring (hard version)

Original title link

Subject to the effect of the problem exactly the same, but on a larger scale data.

 

Ideas:

And then on the question of violence is certainly TLE, so it needs to optimize the search process. The outer layer is a question we iterate n, traversing the inner three cases. This problem we traverse the outer three cases, the inner layer through the array, where each point is recorded, the latter point may utilize the foregoing results do not always re-counting from zero. Also updated in real time minimum result.

 

Code:

 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 #define MAX 200005
 5 
 6 using namespace std;
 7 
 8 long long q, n, k;
 9 string sset = "RGB";
10 
11 int main()
12 {
13     cin >> q;
14 
15     while (q--)
16     {
17         string s;
18         cin >> n >> k >> s;
19         int ans = MAX;
20 
21         for (int i = 0; i < 3; i++)
22         {
23             vector<int> res(n);
24             int cur = 0;
25             for (int j = 0; j < n; j++)
26             {
27                 res[j] = (s[j] != sset[(j+i)%3]);
28                 cur +=res [j];
29                  if (j> = k)
 30                  {
 31                      cur - = res [j- k];
32                  }
 33                  if (j> = k - 1 )
 34                  {
 35                      years = min (years, cur);
36                  }
 37              }
 38          }
 39          cout << age << endl;
40      }
 41 }

 

Reflection:

VJ's C ++ compiler uses a Microsoft Visual C ++ 2010, using string to add <string>

--- end --- restore content

Guess you like

Origin www.cnblogs.com/liyishan/p/11257515.html