86-masala #include using namespace std;
int main() {
int t;
cin >> t;string s1, s2, o;
while(t--) {
cin >> s1 >> s2;
int l1 = s1.length();
int l2 = s2.length();
int p1 = 0, p2 = 0;
o = "";
while(p1 < l1 && p2 < l2) {
while(p1 < l1 && p2 < l2 && s1[p1] != s2[p2]) {
if(s1[p1] < s2[p2]) o += s1[p1++];
else o += s2[p2++];
}
int c = 0, e = 0;
while(p1+c < l1 && p2+c < l2 && s1[p1+c] == s2[p2+c] && s1[p1+c] == s1[p1]) {
++e;
++c;
}
while(p1+c < l1 && p2+c < l2 && s1[p1+c] == s2[p2+c] && s1[p1+c] <= s1[p1]) ++c;
if(p1+c < l1 && p2+c < l2)
if(s1[p1+c] < s2[p2+c])
while(e--) o += s1[p1++];
else
while(e--) o += s2[p2++];
else
if(p1+c == l1)
while(e--) o += s2[p2++];
else if(p2+c == l2)
while(e--) o += s1[p1++];
}
while(p1 < l1) o += s1[p1++];
while(p2 < l2) o += s2[p2++];
cout << o << endl;
}
}
87-masala #include #include
using namespace std;
int main() {
long long n;
cin >> n;
vector a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
long long d, m;
cin >> d >> m;
long long k = 0;
for (int i = 0; i <= n - m; ++i) {
long long sum = 0;
for (int j = i; j < i + m; ++j) {
sum += a[j];
}
if (sum == d) {
k += 1;
}
}
cout << k << endl;
return 0;
}
88-masala #include using namespace std;
int subsetPairNotDivisibleByK(int arr[], int N,int K){
int f[K];
memset(f, 0, sizeof(f));
for (int i = 0; i < N; i++)
f[arr[i] % K]++;
if (K % 2 == 0)
f[K/2] = min(f[K/2], 1);
int res = min(f[0], 1);
for (int i = 1; i <= K/2; i++)
res += max(f[i], f[K-i]);
return res;
}
int main(){
int N,k;
cin >> N >> k;
int arr[N];
for(int i = 0; i < N ; i++){
cin >> arr[i];
}
cout << subsetPairNotDivisibleByK(arr, N, k);
return 0;
}
89-masala #include #define ll long int
using namespace std;
sort(a,a+n);
s[0] = 0;
for(int i = 1; i <= n; i++) s[i] = s[i-1]+a[i-1];
ll t = 0;
for(int i = 0; i < k; i++) t += s[k]-s[i]-(k-i)*a[i];
ll m = t;
for(int i = 0; i < n-k; i++){
t += (k-1)*(a[i]+a[k+i])-2*(s[k+i]-s[i+1]);
m = min(m,t);
}
cout << m;
}
90-masala #include #include int main()
{
long n;
scanf("%li", &n);
long m;
scanf("%lli", &m);
m -= 1;
int i;
long *a = (long *)malloc(n * sizeof(long));
long *b = (long *)malloc(n * sizeof(long));
long *t;
for(i=0; i{
scanf("%li", a+i);
}
long ans = 1;
while (m > 0)
{
if (m & 1)
{
for(i=0; i{
if (i+ans < n)
{
b[i] = a[i] ^ a[i+ans];
}
else
{
b[i] = a[i] ^ a[i+ans-n];
}
}
t = a;
a = b;
b = t;
}
ans <<= 1;
if (ans >= n)
{
ans-= n;
}
m >>= 1;
}
printf("%li", a[0]);
for(i=1; i{
printf(" %li", a[i]);
}
printf("\n");
}
91-masala #include using namespace std ;
#define ft first
#define sd second
#define pb push_back
#define all(x) x.begin(),x.end()
#define ll long long int
#define vi vector #define vii vector
>
#define pii pair #define plii pair
, int>
#define piii pair
#define prll2(x,y) printf("%lld %lld\n",x,y)
#define prll3(x,y,z) printf("%lld %lld %lld\n",x,y,z)
#define pr_vec(v) for(int i=0;i#define f_in(st) freopen(st,"r",stdin)
#define f_out(st) freopen(st,"w",stdout)
#define fr(i, a, b) for(i=a; i<=b; i++)
#define fb(i, a, b) for(i=a; i>=b; i--)
#define ASST(x, l, r) assert( x <= r && x >= l )
#include
#include
const int mod = 1e9 + 7;
int ADD(int a, int b, int m = mod) {
int s = a;
s += b;
if( s >= m )
s -= m;
return s;
}
int MUL(int a, int b, int m = mod) {
return (1LL * a * b % m);
}
int power(int a, int b, int m = mod) {
int res = 1;
while( b ) {
if( b & 1 ) {
res = 1LL * res * a % m;
}
a = 1LL * a * a % m;
b /= 2;
}
return res;
}
ll nC2(ll x) {
return ( x * ( x - 1 ) / 2 );
}
const int maxn = 1e6 + 5;
char s1[ maxn ], s2[ maxn ], s[ maxn ];
int p1[ maxn ], p2[ maxn ];
int n, m, len, num, suff, fd[ maxn ], bd[ maxn ]; // node 1 - root with len -1, node 2 - root with len 0 , max suffix palindrome
char txt[ maxn ];
int iSA[maxn], SA[maxn]; //output
int cnt[maxn], next_gen[maxn], lcp[maxn], LCP[maxn][22], sa1[ maxn ], sa2[ maxn ]; //internal
bool bh[maxn], b2h[maxn];
void reset( int len ) {
int i; fr(i, 0, len) {
iSA[i] = 0;
SA[i] = 0;
cnt[i] = 0;
next_gen[i] = 0;
lcp[i] = 0;
bh[i] = 0;
b2h[i] = 0;
int j;
fr(j, 0, 20)
LCP[i][j] = 0;
}
}
bool smaller_first_char(int a, int b){
return txt[a] < txt[b];
}
void SuffixSort(int n) {
for (int i=0; iSA[i] = i;
}
sort(SA, SA + n, smaller_first_char);
for (int i=0; ibh[i] = i == 0 || txt[SA[i]] != txt[SA[i-1]];
b2h[i] = false;
}
for (int h = 1; h < n; h <<= 1){
int buckets = 0;
for (int i=0, j; i < n; i = j){
j = i + 1;
while (j < n && !bh[j]) j++;
next_gen[i] = j;
buckets++;
}
if (buckets == n) break;
for (int i = 0; i < n; i = next_gen[i]){
cnt[i] = 0;
for (int j = i; j < next_gen[i]; ++j){
iSA[SA[j]] = i;
}
}
cnt[iSA[n - h]]++;
b2h[iSA[n - h]] = true;
for (int i = 0; i < n; i = next_gen[i]){
for (int j = i; j < next_gen[i]; ++j){
int s = SA[j] - h;
if (s >= 0){
int head = iSA[s];
iSA[s] = head + cnt[head]++;
b2h[iSA[s]] = true;
}
}
for (int j = i; j < next_gen[i]; ++j){
int s = SA[j] - h;
if (s >= 0 && b2h[iSA[s]]){
for (int k = iSA[s]+1; !bh[k] && b2h[k]; k++) b2h[k] = false;
}
}
}for (int i=0; iSA[iSA[i]] = i;
bh[i] |= b2h[i];
}
}
for (int i=0; iiSA[SA[i]] = i;
}
}
void InitLCP(int n) {
for (int i=0; iiSA[SA[i]] = i;
lcp[0] = 0;
for (int i=0, h=0; i{
if (iSA[i] > 0)
{
int j = SA[iSA[i]-1];
while (i + h < n && j + h < n && txt[i+h] == txt[j+h])
h++;
lcp[iSA[i]] = h;
if (h > 0)
h--;
}
}
}
void ConstructLCP() {
InitLCP( len );
for(int i = 0;iLCP[i][0] = lcp[i];
for(int j = 1;(1<for(int i = 0;i+(1<if(LCP[i][j-1]<=LCP[i+ ( 1<<(j-1) )][j-1])
LCP[i][j] = LCP[i][j-1];
else
LCP[i][j] = LCP[i+(1<<(j-1))][j-1];
}
}
}
int GetLCP(int x, int y) {
if(x == y) return len-SA[x];
if(x > y) swap(x,y);
int log = 0;
while((1<--log;
return min(LCP[x+1][log],LCP[y-(1<}
struct node {
int next[26];
int len;
int sufflink;
};
node tree[maxn];
bool addLetter(int pos) {
int cur = suff, curlen = 0;
int let = s[pos] - 'a';
while (true) {
curlen = tree[cur].len;
if (pos - 1 - curlen >= 0 && s[pos - 1 - curlen] == s[pos])
break;
cur = tree[cur].sufflink;
}
if (tree[cur].next[let]) {
suff = tree[cur].next[let];
return false;
}
num++;
suff = num;
tree[num].len = tree[cur].len + 2;
tree[cur].next[let] = num;
if (tree[num].len == 1) {
tree[num].sufflink = 2;
return true;
}
while (true) {
cur = tree[cur].sufflink;
curlen = tree[cur].len;
if (pos - 1 - curlen >= 0 && s[pos - 1 - curlen] == s[pos]) {
tree[num].sufflink = tree[cur].next[let];
break;
}
}
return true;
}