求助,急!
  • 板块灌水区
  • 楼主code_hyx
  • 当前回复1
  • 已保存回复1
  • 发布时间2023/9/16 08:31
  • 上次更新2023/11/2 20:19:35
查看原帖
求助,急!
530797
code_hyx楼主2023/9/16 08:31
 #include <iostream> 
 #include <string> 
 #include <vector> 
 
 using namespace std; 
 
 int f(const string &s, const string &t) 
 { 
     int n = s.length(), m = t.length(); 
 
     vector<int> shift(128, m + 1); 
 
     int i, j;
 
     for (j = 0; j < m; j++)
         shift[t[j]] = m - j;
 
     for (i =0; i<= n - m; i += shift[s[i + m]]){
         j =0;
         while(j < m && s[i +j] == t[j]) j++;
         if (j == m) return i;
     }
 
     return -1;
 }
 
 int main()
 {
     string a ,b;
     cin >> a >> b;
     cout << f(a, b) << endl;
     return 0;
 }

去年CSP-S初赛阅读程序题,这不是KMP算法吗?复杂度怎么是O(mn)?

2023/9/16 08:31
加载中...