Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. string virus;
  6.  
  7. int automat[505][26];
  8. int dp[505][505][2];
  9.  
  10. void prec()
  11. {
  12. vector<int> pi(virus.size(),0);
  13. for(int i=1;i<virus.size();i++){
  14. int j = pi[i-1];
  15. while(j>0 && virus[i]!=virus[j]) j=pi[j-1];
  16. if(virus[i]==virus[j]) pi[i]=j+1;
  17. else pi[i]=j;
  18. }
  19.  
  20. for(int i=0;i<virus.size();i++){
  21. for(int j=0;j<26;j++){
  22. char c = (char)(j+'a');
  23. if(i && virus[i]!=c){
  24. automat[i][j]=automat[pi[i-1]][j];
  25. }else{
  26. automat[i][j]=i+(c==virus[i]);
  27. }
  28. }
  29. }
  30. }
  31.  
  32. int solve(string s)
  33. {
  34. int n = s.size();
  35. memset(dp,0,sizeof(dp));
  36. for(int i=0;i<n;i++){
  37. dp[i][0][(i==0?0:1)]=1;
  38. }
  39. for(int i=0;i<n;i++){
  40. for(int j=0;j<virus.size();j++){
  41. for(int flag=0;flag<2;flag++){
  42. for(char c='a';c<='z';c++){
  43. if(flag==0 && c>s[i]) break;
  44. int nflag = (flag==1?1 : c==s[i]?0:1);
  45. int nj = automat[i][c-'a'];
  46. dp[i+1][nj][nflag]+=dp[i][j][flag];
  47. }
  48. }
  49. }
  50. }
  51. int ans=0;
  52. for(int j=0;j<virus.size();j++){
  53. for(int flag=0;flag<2;flag++){
  54. ans+=dp[n][j][flag];
  55. }
  56. }
  57. return ans;
  58. }
  59.  
  60. bool good(string s)
  61. {
  62. bool ok=true;
  63. int n = s.size();
  64. int nn = virus.size();
  65. for(int i=0;i<=n-nn;i++){
  66. string temp;
  67. for(int j=i;j<i+nn;j++){
  68. temp+=s[j];
  69. }
  70. if(temp==virus) ok=false;
  71. }
  72. return ok;
  73. }
  74.  
  75. int main()
  76. {
  77. ios_base::sync_with_stdio(false);
  78. cin.tie(0); cout.tie(0);
  79. ///
  80. string s,t;
  81. cin>>s>>t>>virus;
  82. prec();
  83. cout<<solve(t)-solve(s)-good(s)<<endl;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement