当前位置: 首页 > news >正文

字符串哈希+KMP

P10468 兔子与兔子

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int N = 1000010;
ull a[N], pw[N];
int n;
ull gethash(int l, int r){return a[r] - a[l - 1] * pw[r - l + 1];
}
signed main(){ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);string s; cin >> s;s = " " + s; int base = 131;pw[0] = 1;for(int i = 1; i <= s.size(); i++){a[i] = a[i - 1] * base + (ull)s[i];pw[i] = pw[i - 1] * base;}cin >> n;for(int i = 1; i <= n; i++){int l1, r1, l2, r2; cin >> l1 >> r1 >> l2 >> r2;ull x = gethash(l1, r1), y = gethash(l2, r2);if(x == y)cout << "Yes" << endl;else cout << "No" << endl;} return 0;
}

P3375 【模板】KMP

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
int nex[1000005];
void getnext(char s[], int ls){int j = 0;nex[0] = 0;for(int i = 1; i < ls; i++){while(j > 0 && s[i] != s[j])j = nex[j - 1];if(s[i] == s[j]){j++;}nex[i] = j;}
}
signed main() {ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);char p[1000005], s[1000005];cin >> p;cin >> s;int lp = strlen(p);int ls = strlen(s);getnext(s, ls);int i = 0;int j = 0;while(i < lp){if(p[i] == s[j]){i++, j++;}else{if(j > 0)j = nex[j - 1];else i++;}if(j == ls){cout << i - ls + 1 << endl;j = nex[j - 1];}}for(int i = 0; i < ls; i++)cout << nex[i] << " ";return 0;
}

P4391 [BalticOI 2009] Radio Transmission 无线传输

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
int nex[1000005];
int n;
void getnext(int ls, char s[]){nex[0] = 0;int j = 0;for(int i = 1; i < ls; i++){while(j > 0 && s[i] != s[j])j = nex[j - 1];if(s[i] == s[j])j++;nex[i] = j;}
}
signed main() {ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);cin >> n;char s[1000005];for(int i = 0; i < n; i++)cin >> s[i];getnext(n, s);int ans = nex[n - 1];cout << n - ans << endl;return 0;
}

P1470 [USACO2.3] 最长前缀 Longest Prefix

深搜加记忆化

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
string s, s1;
map<char, vector<string> > mp;
int len, dp[200010];
int dfs(int pos){int ans = 0, b = 0;if(pos == len)return pos;if(dp[pos])return dp[pos];if(mp[s[pos]].empty())return pos;for(int i = 0; i < mp[s[pos]].size(); i++){string s2 = mp[s[pos]][i];int len2 = s2.size();string s3 = s.substr(pos, len2);if(s2 == s3){ans = max(ans, dfs(pos + len2));b = 1;}}if(b)return dp[pos] = ans;else return dp[pos] = pos;
}
signed main() {ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);while(1){cin >> s;if(s == ".")break;mp[s[0]].push_back(s);}s = "";while(cin >> s1)s += s1;len = s.size();cout << dfs(0) << endl; return 0;
}

KMP  还没有调出来

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
string s, s1;
int nex[200010];
int km[205][200010];
int dp[200010];
void kmp(int pos, string st){memset(nex, 0, sizeof(nex));nex[0] = 0;int j = 0;for(int i = 1; i < st.size(); i++){while(j > 0 && st[i] != st[j])j = nex[j - 1];if(st[i] == st[j])j++;nex[i] = j;}j = 0;int i = 0;while(i < s.size()){if(s[i] == st[j])i++, j++;else{if(j > 0)j = nex[j - 1];else i++;}if(j == st.size()){km[pos][i] = 1;j = nex[j - 1];}}
}
signed main() {ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);vector<string> v;while(1){cin >> s;if(s == ".")break;v.push_back(s);}cin >> s;for(int i = 0; i < v.size(); i++)kmp(i, v[i]);dp[0] = 1;for(int i = 1; i <= s.size(); i++){for(int j = 0; j < v.size(); j++){if(km[j][i] && i > v[j].size()){dp[i] = dp[i] || dp[i - v[j].size()];}}}for(int i = s.size(); i >= 0; i--){if(dp[i]){cout << i << endl;return 0;}}return 0;
}

CF25E Test

也是没调出来

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
int nex[100010];
void getnext(string st){memset(nex, 0, sizeof(nex));nex[0] = 0;int j = 0;for(int i = 1; i < st.size(); i++){while(j > 0 && st[i] != st[j])j = nex[j - 1];if(st[i] == st[j])j++;nex[i] = j;}
}
signed main() {ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);string s1, s2, s3; cin >> s1 >> s2 >> s3;getnext(s2);int flag1 = -1;int i = 0, j = 0;while(i < s1.size()){if(s1[i] == s2[j])i++, j++;else{if(j > 0)j = nex[j - 1];else i++;}if(i == s1.size()){flag1 = j;}}string s = s1;if(flag1 == -1)s += s2;else{for(int i = flag1; i < s2.size(); i++)s += s2[i];}int flag2 = -1;getnext(s);i = 0, j = 0;while(i < s.size()){if(s[i] == s3[j])i++, j++;else{if(j > 0)j = nex[j - 1];else i++;}if(i == s.size()){flag2 = j;}}if(flag2 == -1)s += s3;else{for(int i = flag2; i < s3.size(); i++)s += s3[i];}cout << s.size() << endl;return 0;
}

http://www.xdnf.cn/news/954325.html

相关文章:

  • 五.建造者模式
  • SQLAlchemy的子查询subquery()
  • 日本本社企业直招|Java /cobol/C#/PM/PL/Salesforce/AWS/SAP 等,正社员/個人事業主,高度人才+20 分
  • Spring状态机
  • 苍穹外卖-day02
  • 机器人模仿学习调研(二)
  • MySQL 8.0 OCP 英文题库解析(十三)
  • Java求职者面试指南:Spring、Spring Boot、Spring MVC与MyBatis技术解析
  • fpga系列 HDL : Microchip FlashPro 导出与烧录FPGA
  • 6.9本日总结
  • 网络安全A模块专项练习任务六解析
  • python打卡day49@浙大疏锦行
  • 欢乐熊大话蓝牙知识17:多连接 BLE 怎么设计服务不会乱?分层思维来救场!
  • C#中用于控制自定义特性(Attribute)
  • 【Dify】基于 Agent 实现热门新闻生成助手
  • 【教程】矩形重叠检测 -- 分离轴定理的应用
  • Vue 插槽(Slot)用法详解
  • UFW防火墙安全指南
  • 【算法-BFS实现FloodFill算法】使用BFS实现FloodFill算法:高效识别连通块并进行图像填充
  • 时间复杂度和算法选择
  • WinUI3开发_使用mica效果
  • vitepress添加图片放大功能
  • 基于2.4G功能的使用
  • encodeURIComponent和decodeURIComponent
  • 21-Oracle 23 ai-Automatic SQL Plan Management(SPM)
  • 多元隐函数 偏导公式法 (显示变化 + 隐式变化)
  • ABAP设计模式之---“Tell, Don’t Ask原则”
  • STL 1 容器
  • 基于生态系统服务(InVEST模型)的人类活动、重大工程生态成效评估、论文写作
  • 12.找到字符串中所有字母异位词