zoukankan      html  css  js  c++  java
  • Bacterial Melee CodeForces

    大意: 给定字符串, 每次可以任选一个字符$x$, 将$x$左侧或右侧也改为$x$, 求最终能得到多少种字符串.

    首先可以观察到最终字符串将连续相同字符合并后一定是原字符串的子序列

    并且可以观察到相同长度(设为m)的子序列能产生的最终字符串种类数是相同的.

    $inom{n-1}{m-1}$ (可以通过排列后m-1个字符首次出现位置得到).

    然后问题就转化为求原字符串相邻字符不同且本质不同的子序列个数.

    前缀优化一下复杂度为$O(n^2)$

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <math.h>
    #include <set>
    #include <map>
    #include <queue>
    #include <string>
    #include <string.h>
    #include <bitset>
    #define REP(i,a,n) for(int i=a;i<=n;++i)
    #define PER(i,a,n) for(int i=n;i>=a;--i)
    #define hr putchar(10)
    #define pb push_back
    #define lc (o<<1)
    #define rc (lc|1)
    #define mid ((l+r)>>1)
    #define ls lc,l,mid
    #define rs rc,mid+1,r
    #define x first
    #define y second
    #define io std::ios::sync_with_stdio(false)
    #define endl '
    '
    #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
    using namespace std;
    typedef long long ll;
    typedef pair<int,int> pii;
    const int P = 1e9+7, INF = 0x3f3f3f3f;
    ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
    ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
    ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
    //head
    
    
    
    
    const int N = 5e3+10;
    int n, nn, a[N];
    int f[N][27], g[N][27], sum[N], t[N], C[N][N];
    
    int main() {
    	scanf("%d", &n),nn=n;
    	REP(i,1,n) {
    		char c;
    		scanf(" %c", &c);
    		if (c==a[i-1]+'a'-1) --i,--n;
    		else a[i] = c-'a'+1;
    	}
    	REP(i,1,n) {
    		memcpy(sum,t,sizeof t);
    		memcpy(g,f,sizeof f);
    		PER(j,1,i-1) {
    			int r = ((ll)sum[j]-g[j][a[i]]-g[j+1][a[i]])%P;
    			(f[j+1][a[i]]+=r)%=P, (t[j+1]+=r)%=P;
    		}
    		if (!f[1][a[i]]) f[1][a[i]]=1,++t[1];
    	}
    	REP(i,0,nn) {
    		C[i][0]=1;
    		REP(j,1,i) C[i][j]=(C[i-1][j]+C[i-1][j-1])%P;
    	}
    	ll ans = 0;
    	REP(i,1,n) REP(j,1,26) ans+=(ll)C[nn-1][i-1]*f[i][j]%P;
    	printf("%lld
    ", (ans%P+P)%P);
    }
    
  • 相关阅读:
    DOM面试题【三】
    JS面试题【二】
    移动端面试题【一】
    【python】mysql查询错误告警的处理
    硬币排成线
    书籍复印
    分割回文串
    分割回文串 II
    完全平方数
    俄罗斯套娃信封问题
  • 原文地址:https://www.cnblogs.com/uid001/p/10644798.html
Copyright © 2011-2022 走看看