classSolution { public: string shiftingLetters(string s, vector<vector<int>>& shifts){ int n = s.size(); int cnt[n]; memset(cnt, 0, sizeof(cnt));
for(vector<int> a:shifts){ int start=a[0],end=a[1],op=a[2]; if (op==0){ cnt[start]--; if (end+1<n)cnt[end+1]++; } else{ cnt[start]++; if (end+1<n)cnt[end+1]--; } } for(int i = 1;i<n;++i) cnt[i]+=cnt[i-1];
for (int i = 0; i < n; ++i) { cnt[i]+=(s[i]-'a'); while(cnt[i]<0)cnt[i]+=26; if(cnt[i]>=26)cnt[i]%=26; }
string a; for (int i = 0; i < n; ++i) a+=(char)(cnt[i] + 'a');
voidcheck(vector<vector<>int> &mat, unordered_set<int> &set){ int cnt = 0; for (int i = 0; i < mat.size(); ++i) { int j; for (j = 0; j < mat[0].size(); ++j) { if (mat[i][j] == 1) { if (!set.count(j))break; } } if (j == mat[0].size())++cnt; } ans = max(ans, cnt); }
public: voiddfs(vector<vector<int>> &mat, int cols, unordered_set<int> set, int idx){ if (idx == mat[0].size()&&cols!=0)return; if (cols == 0) { check(mat, set); return; } for (int i = idx; i < mat[0].size(); ++i) { set.insert(i); dfs(mat, cols - 1, set, i + 1); set.erase(i); } }
string Binary(int x){ string s = ""; while (x) { if (x % 2 == 0) s = '0' + s; else s = '1' + s; x /= 2; } return s.size()==0 ? "0" : s; }
using vi=vector<int>; using vs=vector<string>;
classSolution { public: vector<int> smallestSubarrays(vector<int>& nums){ int n=nums.size(),maxx=0;
vs s(n); vi ans(n); map<int, set<int>> map;
for (int i = 0; i < n; ++i) { s[i]= Binary(nums[i]); maxx=max(maxx,(int)s[i].size()); }
for (int i = 0; i < n; ++i) { string b=s[i]; int j=b.size()-1; while (j>=0){ if (b[j]=='1'){ map[b.size()-j].insert(i); } --j; } }
for (int i = 0; i < n; ++i) { string target=s[i]; int last=i; int j=maxx-1; while (target.size() < maxx)target.insert(target.begin(), '0'); while (j>=0){ if (target[j]=='0'){ auto it=map[maxx-j].lower_bound(i); if(it!=map[maxx-j].end())last=max(last, *it); } --j; } ans[i]=last-i+1; } return ans; } };
voidadd(string &s){ int now = 0; for (int i = 0; i < s.size(); i++) { f[now]++; int c = s[i] - 'a'; if (ch[now][c] == -1) ch[now][c] = newNode(); now = ch[now][c]; } f[now]++; }
intquery(string &s){ int now = 0, ret = 0; for (int i = 0; i < s.size(); i++) { if (now > 0) ret += f[now]; int c = s[i] - 'a'; now = ch[now][c]; } ret += f[now]; return ret; }
classSolution { typedeflonglong ll; constint mod = 1e9 + 7; public: intnumberOfPaths(vector<vector<int>> &grid, int k){ int m = grid.size(), n = grid[0].size();
ll f[m][n][k]; memset(f, 0, sizeof(f));
f[0][0][grid[0][0] % k] = 1; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { for (int l = 0; l < k; l++) { if (i > 0) f[i][j][l] = (f[i][j][l] + f[i - 1][j][(l - grid[i][j] % k + k) % k]) % mod; if (j > 0) f[i][j][l] = (f[i][j][l] + f[i][j - 1][(l - grid[i][j] % k + k) % k]) % mod; } } } return (int) (f[m - 1][n - 1][0] % mod); } };
classSolution { boolcheck(ll n, int target){ int to = 0; while (n > 0){ to += n % 10; n /= 10; } return to <= target; } public: longlongmakeIntegerBeautiful(longlong n, int target){ if (check(n,target)) return0; ll t = n; ll ans = 0, p = 1; while (!check(n, target)) { ll num = t % 10; t /= 10;
if (num != 0){ n += ((10 - num) * p); ans += p * (10 - num); } p *= 10; t = n / p; } return ans; } };