C. Cow and Message
https://codeforces.com/contest/1307/problem/C
题意:给出一个串,让我们从其中选出最多有多少个相同的下标为等差序列的子序列;
找出最多的个数;
思路:当子序列的长度大于2时,我们会发现,可以用一个长度为2的来替代他;
比如aabbcc序列,我们有两个长度为3的等差序列,1 3 5 或者 2 4 6;
所以有两串abc 那么 我们取其中任意一个序列长度为2的序列,也能得出两个;
所以我们只需要考虑序列长度为1或者2的时候的情况;
因此,我们不需要考虑等差序列带来的影响(因为只有两个数或者一个数的序列肯定为等差)
那么,我们就要枚举长度为1或者2的最大值;
如何枚举呢;
我们可以考虑复杂度为O(26*n)的dp做法;
假如当前位置为a,那么到当前位置,以a为末尾数,以a~z为第一个数的方案数,就可以枚举出来(前面有多少个a,aa的方案数到目前位置九尾就为多少)
枚举完后,我们再将到当前位置有多少个a 记录下来;
这样计算下去枚举到最后就能得出答案
1 #include <iostream> 2 #include <cstring> 3 #include <vector> 4 #include <cmath> 5 #include <algorithm> 6 #include <map> 7 #include <vector> 8 #include <queue> 9 #define INF 0x3f3f3f3f 10 using namespace std; 11 typedef long long ll; 12 string s; 13 ll dp[30][30],a[30]; //dp := double, a := single 14 15 int main(void) 16 { 17 cin>>s; 18 ll _max = 0; 19 for (int i = 0; i < s.length(); i++){ 20 int t = s[i] - 'a'; 21 for (int j = 0; j < 26; j++){ 22 dp[t][j] += a[j]; 23 _max = max(dp[t][j],_max); 24 } 25 a[t]++; //这里不能放上面 26 _max = max(a[t],_max); 27 } 28 cout<<_max<<endl; 29 return 0; 30 }
https://www.jooks.cn/index.php/archives/17/ 代码来自此博客