题解:P17061 [JRKSJ R10 热身赛] Diskontinuierliches
前言:
第二题由于脑子不好成功被区分,要掉大分了。
分析:
刻画一个串合法的条件是:它可以由若干个不连续的子串拼接而成。那么你考虑一个六十分的暴力,首先枚举这个串的位置
考虑若干个不连续子串中,每个字符的位置。显然有不在
现在考虑正解,发现就是把
# include <bits/stdc++.h>
# define int long long
using namespace std ;
string s ;
int c[27][1000005] , cc , sum[1000005] ;
signed main () {
cin >> s ; int n = s.size() ; s = ' ' + s ; int c1 = 0 , c2 = 0 ;
for ( int i = 1 ; i <= n ; ++ i ) c[s[i] - 'a'][i] ++ ;
for ( int i = 1 ; i <= n ; ++ i ) for ( int j = 0 ; j < 26 ; ++ j ) c[j][i] += c[j][i - 1] ;
for ( int i = 1 ; i <= n ; ++ i ) sum[i] = bool ( c[s[i] - 'a'][n] - c[s[i] - 'a'][i] ) ;
for ( int i = n ; i >= 1 ; -- i ) sum[i] += sum[i + 1] ;
for ( int i = 1 ; i <= n ; ++ i )
c1 += bool ( c[s[i] - 'a'][n] - c[s[i] - 'a'][i] ) * ( i - 1 ) ,
c2 += bool ( c[s[i] - 'a'][i - 1] ) * ( n - i - sum[i + 1] ) ;
return cout << c1 + c2 << "\n" , 0 ;
}