java - maximum number of elements which can be added to an arraylist -


question :-

you're given string s of n characters. it's known string consists of lowercase latin letters. string generated randomly. means every symbol chosen randomly , independently others set {'a', 'b', ..., 'z'}. letters has equal probability appear.

you're given q queries on string. each query of form p c, p integer between 1 , n (both inclusive) , c character set {'a', 'b', ..., 'z'}. both p , c chosen @ random , independently other queries.

when have query of form p c have change pth symbol of s c. after every change ask output number of distinct nonempty sub-strings of s.

input format first line of input consists of 2 single space separated integers n , q - length of string s , number of queries respectively.

the second line contains string s itself.

the following q lines describe queries in form p c, p , c separated single space.

constraints

4 ≤ n ≤ 75000 4 ≤ q ≤ 75000

output format output q lines. output number of distinct substrings of s after ith query on ith line of output.

sample input

4 4  aaab 1 2 b 3 c 4 d 

sample output :-

7 7 9 10 

explanation :-

after replacing character @ 1st index a, still have original string aaab. total non empty substrings of aaab are

a b aa ab aaa aab aaab hence 7.

after replacing character @ 2nd index b, have string abab. total non empty substrings of abab are

a b ab ba aba bab abab hence 7.

after replacing character @ 3rd index c, have string abcb. total non empty substrings of abcb are

a b c ab bc cb abc bcb abcb hence 9.

after replacing character @ 4th index d, have string abcd. total non empty substrings of abcd are

a b c d ab bc cd abc bcd abcd hence 10.

my code :-

import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*;  public class solution  {  public static long count(string string)  {     string sub;     int i,c,length;     arraylist<string>al=new arraylist<string>();     length = string.length();      for(c=0;c<length;c++)     {         for(i=1;i<=length-c;i++)         {             sub = string.substring(c,c+i);             al.add(sub);         }     }      hashset hs = new hashset();     hs.addall(al);     al.clear();     al.addall(hs);      return al.size(); } public static void main(string[] args)  {     scanner sc = new scanner(system.in);     int n = sc.nextint();     int q = sc.nextint();      string s = sc.next();     stringbuilder m = new stringbuilder(s);     while((q--)>0)     {         int p = sc.nextint() - 1;         char c = sc.next().tochararray()[0];         m.setcharat(p,c);         string z = m.tostring();         long x = count(z);         system.out.println(x);     }  } } 

when input large , if answer more 2147483647 ( maximum int value ) example 2812324482 .... wrong output

as know size() method returns int it's maximum capacity 2147483647 , answer expected more couldn't accomodated

can give me idea store more value or other alternative manual or predefined method returns size of arraylist maximum capacity more 2147483647?

the maximum number of item contained arraylist depends on implementation. since list<integer> different list<person>, maximum number of items in these 2 list should different.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -