#include <bits/stdc++.h> #define ll long long using namespace std; ll i,n,j,d,m; string…
- User
#include <bits/stdc++.h> #define ll long long using namespace std; ll i,n,j,d,m; string s,t; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); freopen("BAI2.inp","r",stdin); freopen("BAI2.out","w",stdout); cin>>n; //cin.ignore(); cin>>s;s=' '+s; j=n-1; d=int(s[n])-48; while(j>=1&&(s[j]=='9'||d==0)) { d=d+int(s[j])-48; j--; } if(j==0) cout<<-1; else { d=0; for(i=j+1;i<=n;i++) m=m+int(s[i])-48; m--; while(m>9) { t='9'+t; m=m-9; d++; } t=char(m+48)+t;d++; while(d<n-j) { t='0'+t; d++; } char x=s[j]; s=s.substr(1,j-1); s=s+char(int(x)+1); cout<<s<<t; } return 0; } giải thích ý nghĩa chương trình
- Assistant
- User
#include <bits/stdc++.h> using namespace std; #define ll long long const int N = 1e5+5, base = 1e6+9; int n, k, st[base]; ll p[N], H[N]; string s; ll getH(int i, int j) { if (i == 0) return H[j]; ll ans = (H[j]-H[i-1]*p[j-i+1]%base)%base; while (ans < 0) ans += base; return ans; } bool check(int len) { memset(st, 0, sizeof(st)); for(int i = 0; i <= n-len; i++) { ll code = getH(i,i+len-1); if (++st[code] >= k) return true; } return false; }
int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); freopen("BAI4.inp", "r", stdin); freopen("BAI4.out", "w", stdout); cin >> n >> k; cin >> s; p[0] = 1; for(int i = 1; i <= n; i++) { p[i] = p[i-1] * 27 % base; } H[0] = s[0]-'a'+1; for (int i = 1; i< n; i++) { H[i] = (H[i-1]*27 + s[i]-'a'+1) % base; } int ans = -1; int L = 1, R = n; while (L <= R) { int mid = (L+R)/2; if (check(mid)) ans = mid, L = mid+1; else R = mid-1; } cout << ans; return 0; } Giải thích ý nghĩa các câu lệnh trong chương trình
- Assistant