Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- string virus;
- int automat[505][26];
- int dp[505][505][2];
- void prec()
- {
- vector<int> pi(virus.size(),0);
- for(int i=1;i<virus.size();i++){
- int j = pi[i-1];
- while(j>0 && virus[i]!=virus[j]) j=pi[j-1];
- if(virus[i]==virus[j]) pi[i]=j+1;
- else pi[i]=j;
- }
- for(int i=0;i<virus.size();i++){
- for(int j=0;j<26;j++){
- char c = (char)(j+'a');
- if(i && virus[i]!=c){
- automat[i][j]=automat[pi[i-1]][j];
- }else{
- automat[i][j]=i+(c==virus[i]);
- }
- }
- }
- }
- int solve(string s)
- {
- int n = s.size();
- memset(dp,0,sizeof(dp));
- for(int i=0;i<n;i++){
- dp[i][0][(i==0?0:1)]=1;
- }
- for(int i=0;i<n;i++){
- for(int j=0;j<virus.size();j++){
- for(int flag=0;flag<2;flag++){
- for(char c='a';c<='z';c++){
- if(flag==0 && c>s[i]) break;
- int nflag = (flag==1?1 : c==s[i]?0:1);
- int nj = automat[i][c-'a'];
- dp[i+1][nj][nflag]+=dp[i][j][flag];
- }
- }
- }
- }
- int ans=0;
- for(int j=0;j<virus.size();j++){
- for(int flag=0;flag<2;flag++){
- ans+=dp[n][j][flag];
- }
- }
- return ans;
- }
- bool good(string s)
- {
- bool ok=true;
- int n = s.size();
- int nn = virus.size();
- for(int i=0;i<=n-nn;i++){
- string temp;
- for(int j=i;j<i+nn;j++){
- temp+=s[j];
- }
- if(temp==virus) ok=false;
- }
- return ok;
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(0); cout.tie(0);
- ///
- string s,t;
- cin>>s>>t>>virus;
- prec();
- cout<<solve(t)-solve(s)-good(s)<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement