o1-mini-2024-09-12· OpenAI
WildChat-4.8M (AI2)
Z-function Input file: stdin Output file: stdout Time limit: 2 sec Memory limit: 256 mb…
- User
Z-function Input file: stdin
Output file: stdout
Time limit: 2 sec
Memory limit: 256 mb
Compute Z-function for a given string s.
Input Single line contains string s of only Latin letters (1 \le |s| \le 10^6).
Output Output values of the Z-function for string s for indices 2, 3, \ldots, |s|.
Examples Input 1:
copy
aaaAAA
Output 1:
copy
2 1 0 0 0
Input 2:
copy
abacaba
Output 2:
copy
0 1 0 3 0 1 solve in c++ do not use comments. use short naming and proper spacing. use competitive programming style
- Assistant
#include<bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(0); string label, s; getline(cin, label); getline(cin, s); int n = s.length(); vector<int> z(n,0); int l=0, r=0; for(int i=1;i<n;i++){ if(i <= r) z[i] = min(r-i+1, z[i-l]); while(i + z[i] < n && s[z[i]] == s[i + z[i]]) z[i]++; if(i + z[i] -1 > r){ l = i; r = i + z[i] -1; } } cout<<label<<"\n "; for(int i=1;i<n;i++) cout<<z[i]<<(i<n-1?" ":""); }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.