ZOJ 3985 String of CCPC
2017-11-12 19:48
399 查看
Description
BaoBao has just found a string s of length n consisting of 'C' and 'P' in his pocket. As a big fan of the China Collegiate Programming Contest, BaoBao thinks a substring
sisi+1si+2si+3 and sjsj+1sj+2sj+3 are
different ,if and only i ≠ j.
To make this string more valuable, BaoBao decides to buy some characters from a character store. Each time he can buy one 'C' or one 'P' from the store, and insert the character into any position in s.But
everything comes with a cost. If it's the i-th time for BaoBao to buy a character, he will have to spend
i - 1 units of value.
The final value BaoBao obtains is the final value of s minus the total cost of all the characters bought from the store. Please help BaoBao maximize the final value.
The first line contains an integer n (1 ≤ n ≤ 2 ×105) indicating the length of strings.
The second line contains the string s (|s| = n)consisting of 'C' and 'P'.
It's guaranteed that the sum of n over all test cases will not exceed 106.
Output
For each test case output one line containing one integer, indicating the maximum final value BaoBao can obtain.
Sample Input
Sample Output
Hint
For the first sample test case, BaoBao can buy one 'P' (cost 0 value) and change
to "CCPC". So the final value is 1 - 0 = 1.
For the second sample test case, BaoBao can buy one 'C' and one 'P' (cost 0 + 1 = 1 value) and change
to "CCPCCPC". So the final value is 2 - 1 = 1.
For the third sample test case, BaoBao can buy one 'C' (cost 0 value) and change s to "CCPCP". So the final value is 1 - 0 = 1.
It's easy to prove that no strategies of buying and inserting characters can achieve a better result for the sample test cases.
题目大意:
给定一个只含有C或P的字符串, 字符串的价值为其含有"CCPC"的个数, 你可以向其中插入C或者P代价是第i次插入花费为i - 1,现在问最多能得到的价值是多少。
解题思路:
对于插入字符 只有第一次插入时没有花费, 所以我们就对给定的字符串找到插到哪个位置能是的收益最大。 我们又知道对于“CCC”, “CCP”, “CPC”这三种字符串添加一个字符可以额外构造出一个“CCPC”, 那么我们只要在字符串中找到是否有它们即可。
对于字符串的处理顺序十分重要, 我第一次写的时候先插入再找“CCPC”直接WA。正确的处理顺序应该是先找到原字符串中”CCPC“的个数, 我们知道对于每一个找到的“CCPC”, 对于中间的“CP”到后来其实没有任何贡献了, 所以我们去插入的时候只需要考虑首尾的C能否有贡献即可。
代码:
BaoBao has just found a string s of length n consisting of 'C' and 'P' in his pocket. As a big fan of the China Collegiate Programming Contest, BaoBao thinks a substring
sisi+1si+2si+3 and sjsj+1sj+2sj+3 are
different ,if and only i ≠ j.
To make this string more valuable, BaoBao decides to buy some characters from a character store. Each time he can buy one 'C' or one 'P' from the store, and insert the character into any position in s.But
everything comes with a cost. If it's the i-th time for BaoBao to buy a character, he will have to spend
i - 1 units of value.
The final value BaoBao obtains is the final value of s minus the total cost of all the characters bought from the store. Please help BaoBao maximize the final value.
Input
There are multiple test cases. The first line of the input contains an integer T, indicating the number of test cases. For each test case:The first line contains an integer n (1 ≤ n ≤ 2 ×105) indicating the length of strings.
The second line contains the string s (|s| = n)consisting of 'C' and 'P'.
It's guaranteed that the sum of n over all test cases will not exceed 106.
Output
For each test case output one line containing one integer, indicating the maximum final value BaoBao can obtain.
Sample Input
3 3 CCC 5 CCCCP 4 CPCP
Sample Output
1 1 1
Hint
For the first sample test case, BaoBao can buy one 'P' (cost 0 value) and change
to "CCPC". So the final value is 1 - 0 = 1.
For the second sample test case, BaoBao can buy one 'C' and one 'P' (cost 0 + 1 = 1 value) and change
to "CCPCCPC". So the final value is 2 - 1 = 1.
For the third sample test case, BaoBao can buy one 'C' (cost 0 value) and change s to "CCPCP". So the final value is 1 - 0 = 1.
It's easy to prove that no strategies of buying and inserting characters can achieve a better result for the sample test cases.
题目大意:
给定一个只含有C或P的字符串, 字符串的价值为其含有"CCPC"的个数, 你可以向其中插入C或者P代价是第i次插入花费为i - 1,现在问最多能得到的价值是多少。
解题思路:
对于插入字符 只有第一次插入时没有花费, 所以我们就对给定的字符串找到插到哪个位置能是的收益最大。 我们又知道对于“CCC”, “CCP”, “CPC”这三种字符串添加一个字符可以额外构造出一个“CCPC”, 那么我们只要在字符串中找到是否有它们即可。
对于字符串的处理顺序十分重要, 我第一次写的时候先插入再找“CCPC”直接WA。正确的处理顺序应该是先找到原字符串中”CCPC“的个数, 我们知道对于每一个找到的“CCPC”, 对于中间的“CP”到后来其实没有任何贡献了, 所以我们去插入的时候只需要考虑首尾的C能否有贡献即可。
代码:
#include <iostream> #include <sstream> #include <cstdio> #include <algorithm> #include <cstring> #include <iomanip> #include <utility> #include <string> #include <cmath> #include <vector> #include <bitset> #include <stack> #include <queue> #include <deque> #include <map> #include <set> using namespace std; /* *ios::sync_with_stdio(false); */ typedef long long ll; typedef unsigned long long ull; const int inf = 0x7fffffff; const int mod = 1000000; const int Max = (int) 2e5 + 9; int n; bool vis[Max]; string str, t = "CCPC"; int check() { int tt = 0; for (int i = 0; i < n - 2; ++i) { if (str[i] == 'C' && str[i + 1] == 'C' && str[i + 2] == 'P' && str[i + 3] == 'C') { tt++; vis[i + 1] = 1, vis[i + 2] = 1; } } for (int i = 0; i < n - 2; ++i) { // CPC if (str[i] == 'C' && str[i + 1] == 'P' && str[i + 2] == 'C' && !vis[i] && !vis[i + 1]) { tt++; break; } // CCC if (str[i] == 'C' && str[i + 1] == 'C' && str[i + 2] == 'C' && !vis[i + 2]) { tt++; break; } // CCP else if (str[i] == 'C' && str[i + 1] == 'C' && str[i + 2] == 'P' && !vis[i] && !vis[i + 1]) { tt++; break; } } return tt; } int main() { int t; ios::sync_with_stdio(false); // freopen("input.txt", "r", stdin); while (cin >> t) { while (t--) { cin >> n; cin >> str; memset(vis, 0, sizeof(vis)); cout << check() << endl; } } return 0; }[/code]
相关文章推荐
- ZOJ - 3985 String of CCPC (2017CCPC秦皇岛站 简单题)
- ZOJ 3985 && 2017CCPC秦皇岛 E:String of CCPC
- ZOJ - 3985 - String of CCPC (分类讨论)
- ZOJ 3985 String of CCPC (字符串判断)
- ZOJ 3985 String Of CCPC 字符串,模拟
- zoj&CCPC秦皇岛站E-思维-String of CCPC
- 2017秦皇岛 - ICPC - E - String of CCPC 【string的一些简单运用,思维】
- 2017 CCPC秦皇岛 E题 String of CCPC
- 2017 ccpc 秦皇岛E String of CCPC
- 2017CCPC秦皇岛 E:String of CCPC
- String of CCPC(CCPC秦皇岛)
- zoj 3784 String of Infinity(难题,方法妙)
- ZOJ 3985 String of CCPC 2017秦皇岛CCPC(子串个数)
- 2017 CCPC 秦皇岛 & ZOJ 3985 - String of CCPC (子串)
- AC自动机,Tarjan(ZOJ 3784,String of Infinity)
- 2017CCPC 秦皇岛现场赛 M-String of CCPC
- C# String.IndexOf 方法
- zoj 1763 A Simple Question of Chemistry(洪水!)
- <LeetCode OJ> 345. Reverse Vowels of a String
- Could not read document: Cannot deserialize instance of `java.lang.String` out of START_ARRAY token