gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
input n then n stings print longest common substring. use hashes. write in c++. do not…
- User
input n then n stings print longest common substring. use hashes. write in c++. do not write comments. use short naming. use bits/stdc abacaba mycabarchive acabistrue
Output 1:
copy
cab
- Assistant
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<string> s(n); for (int i = 0; i < n; i++) cin >> s[i]; string ans; int l = 0, r = min(s[0].size(), 1000); while (l < r) { int m = (l + r + 1) / 2; unordered_set<long long> hs; long long p = 1, base = 31, mod = 1e9 + 7; for (int i = 0; i < m; i++) p = (p * base) % mod; for (int i = 0; i <= s[0].size() - m; i++) { long long h = 0; for (int j = 0; j < m; j++) h = (h * base + s[0][i + j]) % mod; hs.insert(h); for (int j = i + 1; j <= s[0].size() - m; j++) { h = (h * base - (s[0][j - 1] * p) % mod + mod) % mod; h = (h + s[0][j + m - 1]) % mod; hs.insert(h); } } bool found = false; for (const auto &str : s) { unordered_set<long long> h_str; long long h = 0; for (int j = 0; j < m; j++) h = (h * base + str[j]) % mod; for (int j = 0; j <= str.size() - m; j++) { h_str.insert(h); if (j + m < str.size()) { h = (h * base - (str[j] * p) % mod + mod) % mod; h = (h + str[j + m]) % mod; } } if (any_of(hs.begin(), hs.end(), [&](long long hh) { return h_str.count(hh); })) { found = true; break; } } if (found) { ans = s[0].substr(0, m); l = m; } else { r = m - 1; } } cout << ans; return 0; } - User
in same style Prefix Function Input file: stdin
Output file: stdout
Time limit: 2 sec
Memory limit: 256 mb
Compute prefix function for given string .
Input Single line contains a string containing only letters ().
- Assistant
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n = s.size(); vector<int> pi(n); for (int i = 1; i < n; i++) { int j = pi[i - 1]; while (j > 0 && s[j] != s[i]) j = pi[j - 1]; if (s[j] == s[i]) j++; pi[i] = j; } for (int i = 0; i < n; i++) cout << pi[i] << " "; return 0; }