only constraint i see is of
1-string not more then 80 char and strings contain only lowercase letters and underscores(_)
2-k (1-100)
i have made assumption about a1,a2,a3 that is to be equal to size of string(in the case when all char belong to same group ) ,and i am keeping check on it by 3 int p,q,r so that it is in the range..