标签:acm
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <map> #include <algorithm> #include <cmath> using namespace std; #define maxn 1000000 string s1; string s2; int go[maxn]; int Hash; int pos[maxn]; int Pow(int a, int b) { if(b == 1) return a; int x = Pow(a, b>>1); long long ans = (long long)x*x; if(b & 1) ans = ans*a; return (int)ans; } void solve() { int len1 = s1.size(); int len2 = s2.size(); int b = 13; int hashp = 0, hasht = 0; for(int i=0; i<len2; i++) { hashp = (hashp * b + (s2[i] - 'a' + 1)) % 1000007; hasht = (hasht * b + (s1[i] - 'a' + 1)) % 1000007; } for(int i=0; i<len1-len2+1; i++) { if(hasht == hashp) printf("%d ", i+1); if(i+len2 < len1) hasht = (hasht*b + (s1[i+len2]-'a'+1) - (s1[i]-'a'+1)*Pow(b, len2)) % 1000007;//注意这里幂函数需要自己写,不然很容易出错 } } int main() { getline(cin, s1); getline(cin, s2); solve(); return 0; }
标签:acm
原文地址:http://blog.csdn.net/dojintian/article/details/45169681