fork(1) 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. Hashing Algorithm
  35.   -------------------------- **/
  36.  
  37. /** -----CUSTOM TYPEDEFS/DEFINES----- **/
  38.  
  39.  
  40. /** -----GLOBAL VARIABLES----- **/
  41. string A, B;
  42. vi hashA;
  43. i64 lenA, lenB;
  44. i64 hashKey = 0, hashMul = 1;
  45. i64 workingKey = 0;
  46.  
  47. /** -----EXTENSIVE FUNCTIONS----- **/
  48.  
  49.  
  50. /** -----COMPULSORY FUNCTIONS----- **/
  51. void VarInput() {
  52. getline(cin, A); getline(cin, B);
  53. lenA = A.size(); lenB = B.size();
  54. hashA.resize(lenA, 0);
  55.  
  56. // Initialize hashMul
  57. for (i64 i=1; i<lenB; i++) hashMul = (hashMul * 257) % MOD;
  58.  
  59. // Calculate hashKey
  60. for (i64 i=0; i<lenB; i++) hashKey = (hashKey * 257 + int(B[i])) % MOD;
  61.  
  62. // Pre-processing hashA array and workingKey
  63. for (i64 i=0; i<lenA; i++) {
  64. hashA[i] = int(A[i]);
  65. if (i < lenB) workingKey = (workingKey * 257 + hashA[i]) % MOD;
  66. }
  67. }
  68.  
  69. void ProSolve() {
  70. // Prefix match
  71. if (workingKey == hashKey) cout << "1 ";
  72.  
  73. // Other subsequences
  74. for (i64 i=1; i<=lenA-lenB; i++) {
  75. workingKey = (workingKey - hashA[i-1] * hashMul) % MOD;
  76. while (workingKey < 0) workingKey += MOD;
  77. workingKey = (workingKey * 257 + hashA[i+lenB-1]) % MOD;
  78. if (workingKey == hashKey) cout << (i+1) << " ";
  79. }
  80. cout << "";
  81. }
  82.  
  83. /** -----MAIN FUNCTION----- **/
  84. int main() {
  85. //freopen("FILE.INP", "r", stdin);
  86. //freopen("FILE.OUT", "w", stdout);
  87. ios_base::sync_with_stdio(0); cin.tie(NULL);
  88. VarInput(); ProSolve(); return 0;
  89. }
Success #stdin #stdout 0s 4200KB
stdin
aaaa
a
stdout
1 2 3 4