传送门:【codeforces】2014-2015 ACM-ICPC, Central Europe Regional Contest (CERC 14) F Vocabulary
题目分析:蒽。。。。这个dp嘛其实也是满简单易懂的,就是要考虑的地方略多一点。。。
设字典序最小的为a串,中间的为b串,最大的为c串。
首先我们预处理出'?'个数在前i个字符位置的个数num[i](三个字符串的都算上)。
然后我们分三种情况讨论:
1.第i个位置之前三个串的字典序相同(每个位置三串的字母都相同),第i+1个位置a[i + 1] < b[i + 1] < c[i + 1]。
2.第i个位置之前a、b串的字典序相同,且b串的字典序小于c串,第i+1个位置a[i + 1] < b[i + 1]。
3.第i个位置之前b、c串的字典序相同,且a串的字典序小于b串,第i+1个位置b[i + 1] < c[i + 1]。
当第i个位置三个字符串的大小可以区分时,后面的'?'都可以随便取了,所以要乘上26^(num[n-1]-num[i]),其中n为三个字符串长度的最大值,字符串下标从0开始。
对于每一种情况,第i个位置开始区分字典序时,就是ans+=dp[x][i-1]*(当前的情况数)*(26^(num[n-1]-num[i]))。
dp[x][i - 1]*(当前的情况数)表示情况x在第i个位置符合情况x的定义的方案数。
剩下的就是无比繁琐的if了。。。。(对我来说是这样。。整个人都不好了,写了两天都要写晕了。。。各种细节问题)
代码如下:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std ; typedef long long LL ; #define rep( i , a , b ) for ( int i = ( a ) ; i < ( b ) ; ++ i ) #define For( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i ) #define rev( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i ) #define clr( a , x ) memset ( a , x , sizeof a ) const int MAXN = 1000005 ; const int mod = 1e9 + 9 ; int pow[MAXN * 3] ; int num[MAXN] ; char a[MAXN] ; char b[MAXN] ; char c[MAXN] ; int na , nb , nc , n ; LL dpa[MAXN] , dpb[MAXN] , dpc[MAXN] ; int sigma ( int a , int b ) { return ( a + b ) * ( b - a + 1 ) / 2 ; } void solve () { int ans = 0 ; scanf ( "%s%s%s" , a , b , c ) ; na = strlen ( a ) ; nb = strlen ( b ) ; nc = strlen ( c ) ; n = max ( max ( na , nb ) , nc ) ; rep ( i , 0 , n ) { num[i] = 0 ; dpa[i] = 0 ; dpb[i] = 0 ; dpc[i] = 0 ; } dpa[0] = 1 ; rep ( i , 0 , n ) { if ( i < na ) { if ( a[i] == '?' ) ++ num[i] ; else a[i] -= 'a' - 1 ; } else a[i] = 0 ; if ( i < nb ) { if ( b[i] == '?' ) ++ num[i] ; else b[i] -= 'a' - 1 ; } else b[i] = 0 ; if ( i < nc ) { if ( c[i] == '?' ) ++ num[i] ; else c[i] -= 'a' - 1 ; } else c[i] = 0 ; } rep ( i , 1 , n ) num[i] += num[i - 1] ; int ans1 = 0 , ans2 = 0 , ans3 = 0 ; rep ( i , 0 , n ) { int Pow = pow[num[n - 1] - num[i]] ; int cnt = ( a[i] == '?' ) + ( b[i] == '?' ) + ( c[i] == '?' ) ; if ( cnt == 3 ) { ans1 = ( ans1 + dpa[i] * 2600 % mod * Pow ) % mod ; dpa[i + 1] = dpa[i] * 26 % mod ; } if ( cnt == 2 ) { if ( a[i] != '?' ) { ans1 = ( ans1 + dpa[i] * sigma ( 1 , 26 - a[i] - 1 ) % mod * Pow ) % mod ; if ( a[i] == 0 ) break ; dpa[i + 1] = dpa[i] ; } if ( b[i] != '?' ) { if ( b[i] == 0 ) break ; ans1 = ( ans1 + dpa[i] * ( b[i] - 1 ) * ( 26 - b[i] ) % mod * Pow ) % mod ; dpa[i + 1] = dpa[i] ; } if ( c[i] != '?' ) { if ( c[i] == 0 ) break ; ans1 = ( ans1 + dpa[i] * sigma ( 1 , c[i] - 2 ) % mod * Pow ) % mod ; dpa[i + 1] = dpa[i] ; } } if ( cnt == 1 ) { if ( a[i] == '?' ) { if ( b[i] == 0 || c[i] == 0 ) break ; if ( b[i] < c[i] ) ans1 = ( ans1 + dpa[i] * ( b[i] - 1 ) % mod * Pow ) % mod ; if ( b[i] != c[i] ) break ; dpa[i + 1] = dpa[i] ; } if ( b[i] == '?' ) { if ( c[i] == 0 ) break ; if ( a[i] < c[i] ) ans1 = ( ans1 + dpa[i] * ( c[i] - a[i] - 1 ) % mod * Pow ) % mod ; if ( a[i] == 0 ) break ; if ( a[i] != c[i] ) break ; dpa[i + 1] = dpa[i] ; } if ( c[i] == '?' ) { if ( b[i] == 0 ) break ; if ( a[i] < b[i] ) ans1 = ( ans1 + dpa[i] * ( 26 - b[i] ) % mod * Pow ) % mod ; if ( a[i] == 0 ) break ; if ( a[i] != b[i] ) break ; dpa[i + 1] = dpa[i] ; } } if ( cnt == 0 ) { if ( a[i] < b[i] && b[i] < c[i] ) ans1 = ( ans1 + dpa[i] * Pow ) % mod ; if ( a[i] != b[i] || b[i] != c[i] ) break ; dpa[i + 1] = dpa[i] ; } } rep ( i , 0 , n ) { int Pow = pow[num[n - 1] - num[i]] ; int cnt = ( a[i] == '?' ) + ( b[i] == '?' ) + ( c[i] == '?' ) ; if ( cnt == 3 ) { ans2 = ( ans2 + dpb[i] * sigma ( 1 , 25 ) * 26 % mod * Pow ) % mod ; dpb[i + 1] = ( dpb[i] * 26 * 26 + dpa[i] * sigma ( 1 , 25 ) ) % mod ; } if ( cnt == 2 ) { if ( a[i] != '?' ) { ans2 = ( ans2 + dpb[i] * ( 26 - a[i] ) * 26 % mod * Pow ) % mod ; if ( a[i] == 0 ) break ; dpb[i + 1] = ( dpb[i] * 26 + dpa[i] * ( 26 - a[i] ) ) % mod ; } if ( b[i] != '?' ) { if ( b[i] == 0 ) break ; ans2 = ( ans2 + dpb[i] * ( b[i] - 1 ) * 26 % mod * Pow ) % mod ; dpb[i + 1] = ( dpb[i] * 26 + dpa[i] * ( 26 - b[i] ) ) % mod ; } if ( c[i] != '?' ) { ans2 = ( ans2 + dpb[i] * sigma ( 1 , 25 ) % mod * Pow ) % mod ; if ( c[i] > 1 ) dpb[i + 1] = ( dpb[i] * 26 + dpa[i] * ( c[i] - 1 ) ) % mod ; else dpb[i + 1] = dpb[i] * 26 % mod ; } } if ( cnt == 1 ) { if ( a[i] == '?' ) { if ( b[i] == 0 ) break ; ans2 = ( ans2 + dpb[i] * ( b[i] - 1 ) % mod * Pow ) % mod ; if ( b[i] < c[i] ) dpb[i + 1] = ( dpb[i] + dpa[i] ) % mod ; else dpb[i + 1] = dpb[i] ; } if ( b[i] == '?' ) { ans2 = ( ans2 + dpb[i] * ( 26 - a[i] ) % mod * Pow ) % mod ; if ( a[i] == 0 ) break ; if ( a[i] < c[i] ) dpb[i + 1] = ( dpb[i] + dpa[i] ) % mod ; else dpb[i + 1] = dpb[i] ; } if ( c[i] == '?' ) { if ( a[i] < b[i] ) ans2 = ( ans2 + dpb[i] * 26 % mod * Pow ) % mod ; if ( a[i] != b[i] ) break ; dpb[i + 1] = ( dpb[i] * 26 + dpa[i] * ( 26 - b[i] ) ) % mod ; } } if ( cnt == 0 ) { if ( a[i] < b[i] ) ans2 = ( ans2 + dpb[i] * Pow ) % mod ; if ( a[i] != b[i] ) break ; if ( b[i] < c[i] ) dpb[i + 1] = ( dpb[i] + dpa[i] ) % mod ; else dpb[i + 1] = dpb[i] ; } } rep ( i , 0 , n ) { int Pow = pow[num[n - 1] - num[i]] ; int cnt = ( a[i] == '?' ) + ( b[i] == '?' ) + ( c[i] == '?' ) ; if ( cnt == 3 ) { ans3 = ( ans3 + dpc[i] * sigma ( 1 , 25 ) * 26 % mod * Pow ) % mod ; dpc[i + 1] = ( dpc[i] * 26 * 26 + dpa[i] * sigma ( 1 , 25 ) ) % mod ; } if ( cnt == 2 ) { if ( a[i] != '?' ) { ans3 = ( ans3 + dpc[i] * sigma ( 1 , 25 ) % mod * Pow ) % mod ; dpc[i + 1] = ( dpc[i] * 26 + dpa[i] * ( 26 - a[i] ) ) % mod ; } if ( b[i] != '?' ) { ans3 = ( ans3 + dpc[i] * 26 * ( 26 - b[i] ) % mod * Pow ) % mod ; if ( b[i] == 0 ) break ; dpc[i + 1] = ( dpc[i] * 26 + dpa[i] * ( b[i] - 1 ) ) % mod ; } if ( c[i] != '?' ) { if ( c[i] == 0 ) break ; ans3 = ( ans3 + dpc[i] * 26 * ( c[i] - 1 ) % mod * Pow ) % mod ; dpc[i + 1] = ( dpc[i] * 26 + dpa[i] * ( c[i] - 1 ) ) % mod ; } } if ( cnt == 1 ) { if ( a[i] == '?' ) { if ( b[i] < c[i] ) ans3 = ( ans3 + dpc[i] * 26 % mod * Pow ) % mod ; if ( b[i] != c[i] ) break ; dpc[i + 1] = ( dpc[i] * 26 + dpa[i] * ( b[i] - 1 ) ) % mod ; } if ( b[i] == '?' ) { if ( c[i] == 0 ) break ; ans3 = ( ans3 + dpc[i] * ( c[i] - 1 ) % mod * Pow ) % mod ; if ( a[i] < c[i] ) dpc[i + 1] = ( dpc[i] + dpa[i] ) % mod ; else dpc[i + 1] = dpc[i] ; } if ( c[i] == '?' ) { ans3 = ( ans3 + dpc[i] * ( 26 - b[i] ) % mod * Pow ) % mod ; if ( b[i] == 0 ) break ; if ( a[i] < b[i] ) dpc[i + 1] = ( dpc[i] + dpa[i] ) % mod ; else dpc[i + 1] = dpc[i] ; } } if ( cnt == 0 ) { if ( b[i] < c[i] ) ans3 = ( ans3 + dpc[i] * Pow ) % mod ; if ( b[i] != c[i] ) break ; if ( a[i] < b[i] ) dpc[i + 1] = ( dpc[i] + dpa[i] ) % mod ; else dpc[i + 1] = dpc[i] ; } } /*rep ( i , 0 , n ) printf ( "%d " , dpa[i] ) ; printf ( "\n" ) ; rep ( i , 0 , n ) printf ( "%d " , dpb[i] ) ; printf ( "\n" ) ; rep ( i , 0 , n ) printf ( "%d " , dpc[i] ) ; printf ( "\n" ) ; printf ( "%d %d %d\n" , ans1 , ans2 , ans3 ) ;*/ ans = ( ( LL ) ans1 + ans2 + ans3 ) % mod ; printf ( "%d\n" , ans ) ; } int main () { //freopen ( "1.in" , "r" , stdin ) ; //freopen ( "12.out" , "w" , stdout ) ; int T ; scanf ( "%d" , &T ) ; pow[0] = 1 ; For ( i , 1 , 3000000 ) pow[i] = pow[i - 1] * 26LL % mod ; while ( T -- ) solve () ; return 0 ; }