Advertisement
a53

sufixe

a53
Feb 23rd, 2020
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <cstdio>
  2. #define NMax 1200008
  3. using namespace std;
  4. char s[NMax];
  5. bool isInSet[NMax];
  6. int nextState[NMax], opType[NMax], lenAfterOp[NMax], sol[NMax];
  7.  
  8. void read_ops(int &T, char *s, int &N)
  9. {
  10. scanf("%d %d\n", &N, &T);
  11. scanf("%s\n", s);
  12. for (int i = 0; i < T; ++i)
  13. {
  14. scanf("%d", opType + i);
  15. if (opType[i] == 1)
  16. {
  17. scanf(" %c\n", &s[N++]);
  18. } else if (opType[i] == 2)
  19. {
  20. isInSet[N] = true;
  21. }
  22. lenAfterOp[i] = N;
  23. }
  24. }
  25.  
  26. void build_finite_automata(char *s, int n)
  27. {
  28. nextState[0] = nextState[1] = 0;
  29.  
  30. for (int i = 1, state = 0; i < n; ++i)
  31. {
  32. while (0 < state && s[state] != s[i])
  33. {
  34. state = nextState[state];
  35. }
  36.  
  37. if (s[state] == s[i])
  38. {
  39. state++;
  40. }
  41. nextState[i+1] = state;
  42. }
  43. }
  44.  
  45. void resolve_ops(int T)
  46. {
  47. for (int i = 0; i < T; i++)
  48. {
  49. int len = lenAfterOp[i];
  50. int ans = sol[len] = isInSet[len] + sol[nextState[len]];
  51.  
  52. if (opType[i] == 3)
  53. {
  54. printf("%d\n", ans);
  55. }
  56. }
  57. }
  58.  
  59. int main()
  60. {
  61. int N, T;
  62. freopen("sufixe.in", "r", stdin);
  63. freopen("sufixe.out", "w", stdout);
  64. read_ops(T, s, N);
  65. build_finite_automata(s, N);
  66. resolve_ops(T);
  67. return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement