Yandex.Algorithm 2015 Online Finals E問題 - Compact Strings

Source

Yandex.Algorithm 2015
Yandex.Algorithm 2015 Online Finals
問題文

問題概要

アルファベット小文字のみからなる文字列で,同じ文字は必ず連続しているもの(間に違う文字が入ることはない)をコンパクトと呼ぶ.
アルファベット小文字と $\verb|?|$ のみからなる文字列 $S$ が与えられるので,$\verb|?|$ をアルファベット小文字 $1$ 文字に置き換えてできるコンパクトな文字列は何通りあるかを ${\rm mod}\ 1000000007\ (10^9+7)$ で求める問題.
コンパクトな文字列の例:$\verb|aacccb|$,$\verb|eeeee|$
コンパクトでない文字列の例:$\verb|aba|$,$\verb|aazbbzc|$

解法

各文字について,一番左に出てくる位置と一番右に出てくる位置を求める.
そして,その間に含まれる $\verb|?|$ はその文字に置き換えてしまう.
このとき,区間が重なっている場合は矛盾するので,即座に答えは $0$ として終了.
また,出現しない文字の種類数も数えておく.
後は,左から一文字ずつ見ていきながら,動的計画法でパターン数を数える.
状態は(今何文字目を見ているか,まだ使っていない文字(自由に使える文字)は何種類あるか,最後に置いた文字は既に右に使われているものかどうか)とする.
状態遷移は,前と同じ文字を使う,新しくまだ使っていない文字を置く,直近の右に使われている文字を置く,などという感じ.
文字の種類数を $c = 26$ とすれば,各テストケースごとに時間計算量 $O(c|S|)$ 程度.

C++によるスパゲッティなソースコード

#include<bits/stdc++.h>
using namespace std;

#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)

#define mygc(c) (c)=getchar_unlocked()
#define mypc(c) putchar_unlocked(c)

#define ll long long
#define ull unsigned ll

void reader(int *x){int k,m=0;*x=0;for(;;){mygc(k);if(k=='-'){m=1;break;}if('0'<=k&&k<='9'){*x=k-'0';break;}}for(;;){mygc(k);if(k<'0'||k>'9')break;*x=(*x)*10+k-'0';}if(m)(*x)=-(*x);}
int reader(char c[]){int i,s=0;for(;;){mygc(i);if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF) break;}c[s++]=i;for(;;){mygc(i);if(i==' '||i=='\n'||i=='\r'||i=='\t'||i==EOF) break;c[s++]=i;}c[s]='\0';return s;}

void writer(int x, char c){int s=0,m=0;char f[10];if(x<0)m=1,x=-x;while(x)f[s++]=x%10,x/=10;if(!s)f[s++]=0;if(m)mypc('-');while(s--)mypc(f[s]+'0');mypc(c);}
template<class T> void writerLn(T x){writer(x,'\n');}

#define MD 1000000007

int get_inv(ll a, int md){ll t=a,s=md,u=1,v=0,e;while(s){e=t/s;t-=e*s;u-=e*v;swap(t,s);swap(u,v);}if(u<0)u+=md;return u;}

struct mint{
  static unsigned md, W, R, Rinv, mdninv, RR;
  unsigned val;

  mint(){}mint(int a){val=mulR(a);}mint(unsigned a){val=mulR(a);}mint(ll a){val=mulR(a);}mint(ull a){val=mulR(a);}
  unsigned setmod(unsigned m){int i;unsigned t;W=32;md=m;R=(1ULL<<W)%md;RR=(ull)R*R%md;switch(m){case 104857601:Rinv=2560000;mdninv=104857599;break;case 998244353:Rinv=232013824;mdninv=998244351;break;case 1000000007:Rinv=518424770;mdninv=2226617417U;break;case 1000000009:Rinv=171601999;mdninv=737024967;break;case 1004535809:Rinv=234947584;mdninv=1004535807;break;case 1007681537:Rinv=236421376;mdninv=1007681535;break;case 1012924417:Rinv=238887936;mdninv=1012924415;break;case 1045430273:Rinv=254466304;mdninv=1045430271;break;case 1051721729:Rinv=257538304;mdninv=1051721727;break;default:Rinv=get_inv(R,md);mdninv=0;t=0;rep(i,W){if(t%2==0)t+=md,mdninv|=(1U<<i);t/=2;}}}
  unsigned mulR(unsigned a){return(ull)a*R%md;}unsigned mulR(int a){if(a<0)a=a%md+md;return mulR((unsigned)a);}unsigned mulR(ull a){return mulR((unsigned)(a%md));}unsigned mulR(ll a){if(a<0)a=a%md+md;return mulR((unsigned)a);}
  unsigned reduce(unsigned T){unsigned m=T*mdninv;unsigned t=(unsigned)((T+(ull)m*md)>>W);if(t>=md)t-=md;return t;}unsigned reduce(ull T){unsigned m=(unsigned)T*mdninv;unsigned t=(unsigned)((T+(ull)m*md)>>W);if(t>=md)t-=md;return t;}
  unsigned get(){return reduce(val);}
  mint&operator+=(mint a){val+=a.val;if(val>=md)val-=md;return*this;}mint&operator-=(mint a){if(val<a.val)val=val+md-a.val;else val-=a.val;return*this;}mint&operator*=(mint a){val=reduce((ull)val*a.val);return*this;}mint&operator/=(mint a){return*this*=a.inverse();}
  mint operator+(mint a){return mint(*this)+=a;}mint operator-(mint a){return mint(*this)-=a;}mint operator*(mint a){return mint(*this)*=a;}mint operator/(mint a){return mint(*this)/=a;}
  mint operator+(int a){return mint(*this)+=mint(a);}mint operator-(int a){return mint(*this)-=mint(a);}mint operator*(int a){return mint(*this)*=mint(a);}mint operator/(int a){return mint(*this)/=mint(a);}
  mint operator+(ll a){return mint(*this)+=mint(a);}mint operator-(ll a){return mint(*this)-=mint(a);}mint operator*(ll a){return mint(*this)*=mint(a);}mint operator/(ll a){return mint(*this)/=mint(a);}
  mint operator-(void){mint res;if(val)res.val=md-val;else res.val=0;return res;}
  operator bool(void){return val!=0;}operator int(void){return get();}operator ll(void){return get();}
  mint inverse(){int a=val,b=md,u=1,v=0,t;mint r;while(b){t=a/b;a-=t*b;swap(a,b);u-=t*v;swap(u,v);}if(u<0)u+=md;r.val=(ull)u*RR%md;return r;}
  mint pw(ull b){mint a(*this),r;r.val=R;while(b){if(b&1)r*=a;b>>=1;a*=a;}return r;}
  bool operator==(int a){return get()==a;}
  bool operator!=(int a){return get()!=a;}
};
unsigned mint::md, mint::W, mint::R, mint::Rinv, mint::mdninv, mint::RR;
mint operator+(int a, mint b){return mint(a)+=b;}mint operator-(int a, mint b){return mint(a)-=b;}mint operator*(int a, mint b){return mint(a)*=b;}mint operator/(int a, mint b){return mint(a)/=b;}
mint operator+(ll a, mint b){return mint(a)+=b;}mint operator-(ll a, mint b){return mint(a)-=b;}mint operator*(ll a, mint b){return mint(a)*=b;}mint operator/(ll a, mint b){return mint(a)/=b;}

mint mval[10000], minv[10000];
void mint_init(int md=MD, mint val[]=mval, int vals=10000, mint inv[]=minv, int invs=10000){int i;val[0].setmod(md);val[0].val=0;REP(i,1,vals){val[i].val=val[i-1].val+mint::R;if(val[i].val >=md)val[i].val-=md;}inv[1].val=1;REP(i,2,invs){inv[i].val=md-((ll)(md/i)*inv[md%i].val%md);}REP(i,1,invs)inv[i].val=(ull)inv[i].val*mint::R%md;}

template<class S, class T> inline void chmin(S &a, T b){if(a>b)a=b;}
template<class S, class T> inline void chmax(S &a, T b){if(a<b)a=b;}


int T, N;
char S[200000];

mint dp[60], nx[60];

int mn[26], mx[26];

int main(){
  int i, j, k, ls;
  int f;
  int bef, dame;
  mint res;

  mint_init();

  reader(&T);
  while(T--){
    N = reader(S);
    rep(i,N){
      if(S[i]=='?'){ S[i] = 26; continue; }
      S[i] -= 'a';
    }

    rep(i,26) mn[i] = N+10, mx[i] = -N;
    rep(i,N){
      if(S[i]==26) continue;
      chmin(mn[S[i]], i);
      chmax(mx[S[i]], i);
    }

    dame = 0;
    rep(i,N) if(S[i]==26){
      k = 0;
      rep(j,26){
        if(mn[j] <= i && i <= mx[j]){k++; S[i]=j;}
      }
      if(k>1){dame=1; break;}
    }
    if(dame){writerLn(0); continue;}

    rep(i,N) if(S[i]!=26){
      k = 0;
      rep(j,26) if(j!=S[i]){
        if(mn[j] <= i && i <= mx[j]){k++; break;}
      }
      if(k){dame=1; break;}
    }
    if(dame){writerLn(0); continue;}

    f = 0;
    rep(i,26) if(mn[i] > N) f++;

    ls = N;
    while(ls-1 >= 0 && S[ls-1]==26) ls--;

    rep(i,60) dp[i] = mval[0];
    dp[f] = mval[1];
    rep(k,N){
      if(S[k]==26){
        rep(i,60) nx[i] = mval[0];
        rep(i,27){
          if(k!=0) nx[i] += dp[i];
          if(i) nx[i-1] += dp[i] * mval[i];
          if(k<ls) nx[i+27] += dp[i];
        }
        REP(i,27,60){
          nx[i] += dp[i];
        }
        rep(i,60) dp[i] = nx[i];
      } else {
        REP(i,27,60){
          dp[i-27] += dp[i];
          dp[i] = mval[0];
        }
      }
    }

    res = mval[0];
    rep(i,60) res += dp[i];
    writerLn( (int)res );
  }

  return 0;
}

Current time: 2017年07月21日13時43分45秒
Last modified: 2015年08月15日14時13分26秒 (by laycrs)
Tags: Competitive_Programming Yandex_Algorithm Yandex_Algorithm_2015 Yandex_Algorithm_2015_Finals
トップページに戻る

Logged in as: unknown user (not login)

ログイン: