标签:turn ems 实现 string std 思路 ons mem bsp
思路:
枚举。前缀和优化。
实现:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 const int INF = 0x3f3f3f3f; 6 char a[5005]; 7 int f[15555], sum[15555], n; 8 int solve(int k) 9 { 10 memset(f, 0, sizeof f); 11 memset(sum, 0, sizeof sum); 12 for (int i = 5005; i <= 5005 + n - k; i++) 13 { 14 int tmp = sum[i - 1] - sum[i - k]; 15 if ((tmp % 2 == 0 && a[i - 5005] == ‘B‘) || (tmp % 2 == 1 && a[i - 5005] == ‘F‘)) 16 { 17 f[i] = 1; 18 } 19 sum[i] = sum[i - 1] + f[i]; 20 } 21 for (int i = 5006 + n - k; i < 5005 + n; i++) 22 { 23 sum[i] = sum[i - 1] + f[i]; 24 int tmp = sum[i - 1] - sum[i - k]; 25 if ((tmp % 2 == 0 && a[i - 5005] == ‘B‘) || (tmp % 2 == 1 && a[i - 5005] == ‘F‘)) 26 { 27 return -1; 28 } 29 } 30 return sum[5005 + n - k]; 31 } 32 int main() 33 { 34 cin >> n; 35 for (int i = 0; i < n; i++) 36 { 37 cin >> a[i]; 38 } 39 int minn = INF, res = 0; 40 for (int k = 1; k <= n; k++) 41 { 42 int tmp = solve(k); 43 if (tmp >= 0 && tmp < minn) { minn = tmp; res = k; } 44 } 45 cout << res << " " << minn << endl; 46 return 0; 47 }
标签:turn ems 实现 string std 思路 ons mem bsp
原文地址:http://www.cnblogs.com/wangyiming/p/7620378.html