codeforces535D:Tavas and Malekas(KMP)

(i-1 , i)有重合的时候 ,从第i位开始的子串必须是模式串的前缀。

而同时,从第i位开始的子串本来就已经是模式串的后缀了。


typedef long long LL ;

const  int maxn = 1000008 ;
int   next[maxn] ;

void  getnext(char s[]){
      int len = strlen(s) ;
      next[0] = -1 ;
      int i = 0 ,  j = -1 ;
      while(i < len){
           if(j == -1 || s[i] == s[j])  next[++i] = ++j ;
           else  j = next[j] ;
      }
}

char  word[maxn] ;
int   x[maxn]  ;

const LL  mod = 1000000007LL ;

LL   Pow(LL x , int y){
     LL s = 1  ;
     for(; y ; y >>= 1){
          if(y & 1){
               s *= x ; s %= mod ;
          }
          x *= x ; x %= mod ;
     }
     return s ;
}

int  n , m  , len ;
set<int> st   ;

LL   ans(){
     if(m == 0)  return Pow(26LL , n) ;
     LL sum = Pow(26LL , x[1] - 1) ;
     for(int i = 2 ; i <= m ; i++){
           if(x[i] - x[i-1] >= len){
               sum *= Pow(26LL , x[i] - x[i-1] - len) ;
               sum %= mod ;
           }
           else{
               if(st.find(x[i] - x[i-1] + 1) == st.end()) return 0 ;
           }
     }
     sum *= Pow(26LL , n - x[m] - len + 1) ;
     sum %= mod ;
     return sum ;
}


int  main(){
     while(cin>>n>>m){
           scanf("%s" , word) ;
           getnext(word) ;

           for(int i = 1 ; i <= m ; i++) scanf("%d" , &x[i]) ;
           len = strlen(word) ;
           st.clear() ;
           int i = len ;
           while(next[i] != 0){
                st.insert(len - next[i] + 1) ;
                i = next[i] ;
           }
           cout<< ans() << endl ;
     }
     return 0 ;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值