Palindromic Subsets


Problem Statement :


Consider a lowercase English alphabetic letter character denoted by c. A shift operation on some c turns it into the next letter in the alphabet. For example, and ,shift(a) = b , shift(e) = f, shift(z) = a .

Given a zero-indexed string, s, of n lowercase letters, perform q queries on s where each query takes one of the following two forms:

1 i j t: All letters in the inclusive range from i to j are shifted t times.
2 i j: Consider all indices in the inclusive range from i to j. Find the number of non-empty subsets of characters c1, c2, . . . , ck ,  where , i  <  index of  c1  <  index of c2 < . . . < index of ck <  j )   such that characters  c1, c2, . . . , ck , can be rearranged to form a palindrome. Then print this number modulo 10^9 + 7 on a new line. Two palindromic subsets are considered to be different if their component characters came from different indices in the original string.
Note Two palindromic subsets are considered to be different if their component characters came from different indices in the original string.


Input Format

The first line contains two space-separated integers describing the respective values of n and q.
The second line contains a string of n lowercase English alphabetic letters (i.e., a through z) denoting s.
Each of the q subsequent lines describes a query in one of the two formats defined above.


Output Format

For each query of type 2 (i.e., 2 i j), print the number of non-empty subsets of characters satisfying the conditions given above, modulo 10 ^ 9 + 7, on a new line.



Solution :



title-img


                            Solution in C :

In  C ++ :







#include <algorithm>
#include <cassert>
#include <cstring>
#include <iostream>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define TRACE(x) cout << #x << " = " << x << endl
#define _ << " _ " <<

typedef long long llint;

const int MAX = 100100;
const int off = 1<<17;

const int mod = 1e9 + 7;

inline int add(int a, int b) {
  return a+b >= mod ? a+b-mod : a+b;
}

inline int sub(int a, int b) {
  return a >= b ? a-b : a-b+mod;
}

inline int mul(int a, int b) {
  return llint(a)*b % mod;
}

vector<int> T[2*off];
int S[2*off];

void sh(int i, int x) {
  S[i] = (S[i] + x) % 26;
  rotate(T[i].begin(), T[i].begin() + ((26-x) % 26), T[i].end());
}

void propagate(int i) {
  if (S[i]) {
    sh(i*2, S[i]);
    sh(i*2+1, S[i]);
    S[i] = 0;
  }
}

vector<int> merge(const vector<int>& a, const vector<int>& b) {
  vector<int> c(26);
  REP(i, 26) c[i] = a[i] + b[i];
  return c;
}

void shift(int i, int lo, int hi, int a, int b, int k) {
  if (lo >= b || hi <= a) return;
  if (lo >= a && hi <= b) { sh(i, k); return; }
  propagate(i);
  
  shift(i*2, lo, (lo+hi)/2, a, b, k);
  shift(i*2+1, (lo+hi)/2, hi, a, b, k);

  T[i] = merge(T[i*2], T[i*2+1]);
}
 

vector<int> count(int i, int lo, int hi, int a, int b) {
  if (lo >= b || hi <= a) return vector<int>(26, 0);
  if (lo >= a && hi <= b) return T[i];
  propagate(i);
  return merge(count(i*2, lo, (lo+hi)/2, a, b), count(i*2+1, (lo+hi)/2, hi, a, b));
}

char s[MAX];
int pw[MAX];

int main(void) {
  pw[0] = 1;
  FOR(i, 1, MAX) pw[i] = mul(pw[i-1], 2);
  
  int n, q;
  scanf("%d %d", &n, &q);
  scanf("%s", s);
  REP(i, 2*off) T[i].resize(26);
  REP(i, n) T[off+i][s[i]-'a']++;

  for (int i = off-1; i >= 0; --i) REP(j, 26) T[i][j] = T[2*i][j] + T[2*i+1][j];
                                     
  REP(i, q) {
    int tip;
    scanf("%d", &tip);
    
    if (tip == 1) {
      int a, b, t;
      scanf("%d %d %d", &a, &b, &t);
      t %= 26;
      shift(1, 0, off, a, b+1, t);
    }
    if (tip == 2) {
      int a, b;
      scanf("%d %d", &a, &b);
      vector<int> v = count(1, 0, off, a, b+1);

      int ans = 0;
      REP(j, 27) {
        int ways = 1;
        REP(k, 26)
          if (v[k] && k != j) ways = mul(ways, pw[v[k]-1]);
        if (j != 26) {
          if (v[j] == 0) ways = 0;
          else ways = mul(ways, pw[v[j]-1]);
        }
        ans = add(ans, ways);
      }
      ans = sub(ans, 1);
      printf("%d\n", ans);
    }
  }
  return 0;
}








In  C :






#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

#define M 1000000007

char s[100006];
int ret[32];
int d[1<<18][32];
int f[100006];

void init(int t,int l,int r) {
    int i;
    if (r-l==1) {
        d[t][s[l]-'a']++;
        return ;
    }
    init(t<<1,l,(l+r)/2);
    init(t<<1|1,(l+r)/2,r);
    for(i=0;i<26;i++) d[t][i]=d[t<<1][i]+d[t<<1|1][i];
}

void shift(int a[],int t) {
    int b[32],i;
    for(i=0;i<26;i++) b[(i+t)%26]=a[i];
    for(i=0;i<26;i++) a[i]=b[i];
}

void update(int t,int l,int r,int a,int b,int val) {
    if (l==a && r==b) {
        d[t][26]=(d[t][26]+val)%26;
        return ;
    }
    shift(d[t],d[t][26]);
    d[t<<1][26]+=d[t][26];
    d[t<<1|1][26]+=d[t][26];
    d[t][26]=0;
    if (b<=(l+r)/2) {
        update(t<<1,l,(l+r)/2,a,b,val);
    } else if (a>=(l+r)/2) {
        update(t<<1|1,(l+r)/2,r,a,b,val);
    } else {
        update(t<<1,l,(l+r)/2,a,(l+r)/2,val);
        update(t<<1|1,(l+r)/2,r,(l+r)/2,b,val);
    }
    int i;
    for(i=0;i<26;i++) d[t][i]=0;
    for(i=0;i<26;i++) d[t][(i+d[t<<1][26])%26]+=d[t<<1][i];
    for(i=0;i<26;i++) d[t][(i+d[t<<1|1][26])%26]+=d[t<<1|1][i];
}

void query(int t,int l,int r,int a,int b) {
//    printf("%d %d %d %d %d %d %d\n",t,l,r,d[t][0],d[t][1],d[t][2],d[t][26]);
    if (l==a && r==b) {
        int i;
        for(i=0;i<26;i++) ret[(i+d[t][26])%26]+=d[t][i];
        return ;
    }
    shift(d[t],d[t][26]);
    d[t<<1][26]+=d[t][26];
    d[t<<1|1][26]+=d[t][26];
    d[t][26]=0;
    if (b<=(l+r)/2) {
        query(t<<1,l,(l+r)/2,a,b);
    } else if (a>=(l+r)/2) {
        query(t<<1|1,(l+r)/2,r,a,b);
    } else {
        query(t<<1,l,(l+r)/2,a,(l+r)/2);
        query(t<<1|1,(l+r)/2,r,(l+r)/2,b);
    }
}

int main(){
    int n,m,i;
    scanf("%d %d",&n,&m);
    scanf("%s",s);
    init(1,0,n);
    for(f[0]=i=1;i<=n;i++) if ((f[i]=f[i-1]+f[i-1])>=M) f[i]-=M;
    for(;m--;) {
        int op,l,r,val;
        scanf("%d %d %d",&op,&l,&r);
        if (op==1) {
            scanf("%d",&val);
            update(1,0,n,l,r+1,val);
        } else {
            int d1=1,d2=0;
            memset(ret,0,sizeof(ret));
            query(1,0,n,l,r+1);
         //   printf("%d %d %d\n",ret[0],ret[1],ret[2]);
            for(i=0;i<26;i++) {
                if (!ret[i]) continue;
                d2=(long long)(d2+d1)*f[ret[i]-1]%M;
                d1=(long long)d1*f[ret[i]-1]%M;
            }
            if ((d1+=d2-1)>=M) d1-=M;
            printf("%d\n",d1);
        }
    }
    return 0;
}









In Python3 :




#!/bin/python3

import sys


n,q = input().strip().split(' ')
n,q = [int(n),int(q)]
s = input().strip()
sa = [ord(i) - ord('a') for i in s]
for a0 in range(q):
    qu = input().strip().split(' ')
    if int(qu[0]) == 1:
        start = int(qu[1])
        end =int(qu[2])
        t = int(qu[3])
        for i in range(start, end + 1):
            sa[i] = (sa[i] + t) % 26
    else:
        start = int(qu[1])
        end =int(qu[2])
        d = [0] * 27
        for i in range(start, end + 1):
            d[sa[i]] += 1
        ans = 1
        for i in range(27):
            if d[i]:
                ans = (ans * pow(2, d[i] - 1) )% 1000000007
        g = ans
        ans = (ans - 1 )% 1000000007
        for i in range(27):
            if d[i]:
                ans = (ans + g) % 1000000007

        print(ans)










In Java :





import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class AliceBobSillyGames {


    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int g = in.nextInt();
        for(int a0 = 0; a0 < g; a0++){
            int n = in.nextInt();
            // your code goes here
            getSetArray(n);
        }
    }
    static void getSetArray(int n) {
        int [] numberSet = new int[n];

        for(int i = 1; i <= n; i++) {
            numberSet[i-1] = i;
        }

        int flag = 0;
        for(int i = 2; i <= n; i++) {

            if(isPrime(i)) {
               /* System.out.print(i +" ");*/
                int k = 1;
                while(k <= n) {
                    if(i*k - 1 < n) {
                        numberSet[i*k - 1] = 0;
                    }
                    k++;
                    //k = k*i;
                }
                flag++;
            }
        }


        if(flag % 2 == 0) {
            System.out.println("Bob");
        }
        else {
            System.out.println("Alice");
        }

    }


    static public boolean isPrime(long num) {
        if ( num < 2 ) return false;
        for (int i = 2; i <= Math.sqrt(num); i++) {
            if ( num % i == 0 ) {
                return false;
            }
        }
        return true;
    }
}
                        








View More Similar Problems

Median Updates

The median M of numbers is defined as the middle number after sorting them in order if M is odd. Or it is the average of the middle two numbers if M is even. You start with an empty number list. Then, you can add numbers to the list, or remove existing numbers from it. After each add or remove operation, output the median. Input: The first line is an integer, N , that indicates the number o

View Solution →

Maximum Element

You have an empty sequence, and you will be given N queries. Each query is one of these three types: 1 x -Push the element x into the stack. 2 -Delete the element present at the top of the stack. 3 -Print the maximum element in the stack. Input Format The first line of input contains an integer, N . The next N lines each contain an above mentioned query. (It is guaranteed that each

View Solution →

Balanced Brackets

A bracket is considered to be any one of the following characters: (, ), {, }, [, or ]. Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of matched pairs of brackets: [], {}, and (). A matching pair of brackets is not balanced if the set of bra

View Solution →

Equal Stacks

ou have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height. You can change the height of a stack by removing and discarding its topmost cylinder any number of times. Find the maximum possible height of the stacks such that all of the stacks are exactly the same height. This means you must remove zero or more cylinders from the top of zero or more of

View Solution →

Game of Two Stacks

Alexa has two stacks of non-negative integers, stack A = [a0, a1, . . . , an-1 ] and stack B = [b0, b1, . . . , b m-1] where index 0 denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack A or stack B. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified f

View Solution →

Largest Rectangle

Skyline Real Estate Developers is planning to demolish a number of old, unoccupied buildings and construct a shopping mall in their place. Your task is to find the largest solid area in which the mall can be constructed. There are a number of buildings in a certain two-dimensional landscape. Each building has a height, given by . If you join adjacent buildings, they will form a solid rectangle

View Solution →