fork download
  1. /**
  2. Template by Akikaze (秋風) - formerly proptit_4t41.
  3. Code written by a random fan of momocashew and Chiho.
  4. **/
  5.  
  6. #include <bits/stdc++.h>
  7. using namespace std;
  8.  
  9. /** -----BASIC MACROES----- **/
  10. #define endl '\n'
  11. #define i64 long long
  12. #define ld long double
  13. #define pub push_back
  14. #define mp make_pair
  15. #define fi first
  16. #define se second
  17. const long long MOD = 1000000007LL, INF = 1e9, LINF = 1e18;
  18. const long double PI = 3.141592653589793116, EPS = 1e-9, GOLD = ((1+sqrt(5))/2);
  19. typedef vector<i64> vi;
  20. typedef vector<ld> vd;
  21. typedef vector<string> vs;
  22. typedef vector<bool> vb;
  23. typedef pair<i64, i64> pii;
  24. typedef pair<i64, pii> pip;
  25. typedef pair<pii, i64> ppi;
  26.  
  27. /** -----BIT CONTROLS----- **/
  28. template<class T> int getbit(T s, int i) { return (s >> 1) & 1; }
  29. template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
  30. template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
  31. template<class T> int cntbit(T s) { return __builtin_popcount(s); }
  32.  
  33. /** -----IDEAS/ALGORITHMS-----
  34. Knuth-Morris-Pratt Algorithm
  35.   -------------------------- **/
  36.  
  37. /** -----CUSTOM TYPEDEFS/DEFINES----- **/
  38.  
  39.  
  40. /** -----GLOBAL VARIABLES----- **/
  41. string A, B; i64 lenA, lenB;
  42. vi LPS; bool containAnswer = false;
  43.  
  44. /** -----EXTENSIVE FUNCTIONS----- **/
  45.  
  46.  
  47. /** -----COMPULSORY FUNCTIONS----- **/
  48. void VarInput() {
  49. getline(cin, A); getline(cin, B);
  50. lenA = A.size(); lenB = B.size();
  51.  
  52. // Processing lookup table
  53. LPS.resize(lenB, 0); i64 length = 0;
  54. for (i64 i=1; i<lenB; i++) {
  55. while (length > 0 && B[length] != B[i]) length = LPS[length-1];
  56. if (B[length] == B[i]) LPS[i] = ++length;
  57. else LPS[i] = 0;
  58. }
  59. }
  60.  
  61. void ProSolve() {
  62. i64 pointer = 0;
  63. for (i64 i=0; i<lenA; i++) {
  64. while (pointer > 0 && B[pointer] != A[i]) pointer = LPS[pointer-1];
  65. if (A[i] == B[pointer]) {
  66. pointer++;
  67. if (pointer == lenB) {
  68. if (containAnswer) cout << " ";
  69. cout << (i - lenB + 2);
  70. pointer = LPS[pointer-1];
  71. containAnswer = true;
  72. }
  73. }
  74. }
  75. cout << "";
  76. }
  77.  
  78. /** -----MAIN FUNCTION----- **/
  79. int main() {
  80. //freopen("FILE.INP", "r", stdin);
  81. //freopen("FILE.OUT", "w", stdout);
  82. ios_base::sync_with_stdio(0); cin.tie(NULL);
  83. VarInput(); ProSolve(); return 0;
  84. }
Success #stdin #stdout 0s 4344KB
stdin
aaaaa
aa
stdout
1 2 3 4