设为首页 加入收藏

TOP

Codeforces 113B Petr# 字符串hash
2015-07-24 05:23:23 来源: 作者: 【 】 浏览:5
Tags:Codeforces 113B Petr# 字符串 hash

题目链接:点击打开链接


#include 
  
   
#include 
   
     #include 
    
      #include 
     
       using namespace std; typedef unsigned long long ll; const int key = 1e9 + 7; const int N = 2000 + 2; ll xp[N], h[N]; char a[N], b[N], c[N]; int la, lb, lc; vector
      
        posl; vector
       
         H; ll Hash(int s, int len) { return h[s] - h[s + len] * xp[len]; } void make(char* s, ll* h, int len) { xp[0] = 1; for (int i = 1; i < N; ++i) xp[i] = xp[i - 1] * key; h[len] = 0; for (int i = len - 1; i >= 0; --i) h[i] = h[i + 1] * key + s[i]; } int main() { ll hb = 0, hc = 0; scanf("%s%s%s", a, b, c); la = strlen(a); lb = strlen(b); lc = strlen(c); for (int i = lb - 1; i >= 0; --i) hb = hb * key + b[i]; for (int i = lc - 1; i >= 0; --i) hc = hc * key + c[i]; make(a, h, la); for (int i = 0; i + lc <= la; ++i) if (Hash(i, lc) == hc) posl.push_back(i); for (int i = 0; i + lb <= la; ++i) if (Hash(i, lb) == hb) { for (int j = 0; j < posl.size(); ++j) { if (posl[j] < i || posl[j] + lc < i + lb) continue; H.push_back(Hash(i, posl[j] + lc - i)); } } sort(H.begin(), H.end()); int ans = 0; if (H.size() > 0) ans = 1; for (int i = 1; i < H.size(); ++i) if (H[i] != H[i - 1]) ++ ans; printf("%d\n", ans); return 0; }
       
      
     
    
   
  


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Codeforces 234 F. Fence 下一篇(十三)unity4.6学习Ugui中文文..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: