Codeforces Global Round 26 D ''a'' String Problem(思维)
这题思维性很强,没搞出来,纯记录一下。补充了几个例子帮助理解。思路可以参考Codeforces Global Round 26 (A - E) - Lu_xZ - 博客园 (cnblogs.com)
1 #define IO std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0) 2 #define bug2(x,y) cout<<#x<<" is "<<x<<" "<<#y<<" is "<<y<<endl 3 #define bug(x) cout<<#x<<" is "<<x<<endl; 4 #include<bits/stdc++.h> 5 #define eb emplace_back 6 using namespace std; 7 8 using ll = long long; 9 10 void solve() { 11 string s; 12 cin >> s; 13 int n = s.length(); 14 if(count(s.begin(), s.end(), 'a') == n) { 15 cout << n - 1 << '\n'; 16 return; 17 } 18 vector<int> a; 19 for(int i = 0; i < n; ++ i) { 20 if(s[i] != 'a') { 21 a.eb(i); 22 } 23 } 24 int m = a.size(); 25 ll ans = 0; 26 for(int i = 1; i <= m; ++ i) { 27 if(m % i) { 28 continue; 29 } 30 int ok = 1; 31 for(int j = i; j < m; ++ j) { 32 int o = j % i; 33 if(s[a[j]] != s[a[o]] || (o && a[o] - a[o - 1] != a[j] - a[j - 1])) { 34 ok = 0; 35 break; 36 } 37 } 38 if(ok) { 39 int mi = n; 40 for(int j = i; j < m; j += i) { 41 mi = min(mi, a[j] - a[j - 1] - 1); 42 } 43 int r = n - a.back() - 1; 44 // bug(i); 45 // bug(mi); 46 // bug(r); 47 for(int l = 0; l <= a[0]; ++ l) { 48 //ans += r+1; 49 ans += max(0, min(r + 1, mi - l + 1)); 50 bug2(l,ans); 51 } 52 //bug(i); 53 //bug(ans); 54 } 55 } 56 cout << ans << '\n'; 57 } 58 59 int main() { 60 //cin.tie(0)->sync_with_stdio(0); 61 62 int T; 63 cin >> T; 64 65 while(T --) { 66 solve(); 67 } 68 return 0; 69 } 70 /* 71 10 72 abcaaabcaabcaa //最好的例子,前面枚举l个a,结尾的a的数量会跟着受限制 73 abaacaaadaabaaacaadaaa 枚举i==3时的非法情况 74 abaacaaadaabaacaaadaaa 合法情况 75 */