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

Insert a Node at the head of a Linked List

Given a pointer to the head of a linked list, insert a new node before the head. The next value in the new node should point to head and the data value should be replaced with a given value. Return a reference to the new head of the list. The head pointer given may be null meaning that the initial list is empty. Function Description: Complete the function insertNodeAtHead in the editor below

View Solution →

Insert a node at a specific position in a linked list

Given the pointer to the head node of a linked list and an integer to insert at a certain position, create a new node with the given integer as its data attribute, insert this node at the desired position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is e

View Solution →

Delete a Node

Delete the node at a given position in a linked list and return a reference to the head node. The head is at position 0. The list may be empty after you delete the node. In that case, return a null value. Example: list=0->1->2->3 position=2 After removing the node at position 2, list'= 0->1->-3. Function Description: Complete the deleteNode function in the editor below. deleteNo

View Solution →

Print in Reverse

Given a pointer to the head of a singly-linked list, print each data value from the reversed list. If the given list is empty, do not print anything. Example head* refers to the linked list with data values 1->2->3->Null Print the following: 3 2 1 Function Description: Complete the reversePrint function in the editor below. reversePrint has the following parameters: Sing

View Solution →

Reverse a linked list

Given the pointer to the head node of a linked list, change the next pointers of the nodes so that their order is reversed. The head pointer given may be null meaning that the initial list is empty. Example: head references the list 1->2->3->Null. Manipulate the next pointers of each node in place and return head, now referencing the head of the list 3->2->1->Null. Function Descriptio

View Solution →

Compare two linked lists

You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0. Example: list1=1->2->3->Null list2=1->2->3->4->Null The two lists have equal data attributes for the first 3 nodes. list2 is longer, though, so the lis

View Solution →