ABREPEAT - Editorial

@vijju123 @dpraveen

Why my solution is not working
here is my approach:
what I did is I calculated the no. of occurrences of sub-sequence “ab” in s and say it x.
Then ans will be ans=x*(k*(k+1)/2).

@arpit728 How will you deal with case of-

“baa” repeated twice == “baabaa”

Your x is 0, so your ans reduces to 0, but there are occurances of ab in the final form.

The concept of trailing 'a’s has actually given a headache to many. As a hint I will say that there is a A.P. perspective possible to the problem. The number of “ab” formed by a particular a in FINAL string is an AP with common difference = No. of b in original string.

Summing for all a, we get the answer.:slight_smile:

Watch this code

Ezio’s Python

Program

t=int(input())
while t>0:
count=0
t=t-1
n,k=input().split()
n,k=int(n),int(k)
a=list(input())
a=a*k
for i in range(len(a)):
if a[i]==‘a’:
for j in range(i,len(a)):
if a[j]==‘b’:
count=count+1
print(count)

int main()

{

int t;
cin >> t;
while(t–)
{

     unsigned ll n,k,b_count=0,ab_count=0;
     cin >> n >> k;
     string a,b;
     cin >> a;
     b=a;
    ll i;
    for(i=1;i<=k-1;i++)
    {
        a += b;
    }
    //cout << a;
    for(i=0;i<a.length();i++)
    {
        if( a[i] == 'b' )
            b_count = ( unsigned ll)b_count + 1;
    }
    for(i=0;i<a.length();i++)
    {
        if( a[i] == 'b' )
            b_count =  (unsigned ll)b_count - 1;
        else if( a[i] == 'a' )
            ab_count =  (unsigned ll) ab_count + b_count;
    }
    printf("%llu",ab_count);
    nl;


}

return 0;

}

Can anyone please explain to me why (k-1)/2 has been multiplied? would not only (K * cnta * cntb) be enough?

take a look at this approach …
first we calculate number of b’s after each ‘a’ in the original string and also the total number off b’s in the original string …

eg. let’s take a string abcbabmb … for first “a” , no. of b’s after it’s occurence = 4 and for second “a” , no. of b’s after it’s occurence = 2 .

now , if k==2 , concatenated string will be abcbabmbabcbabmb
for calculating the number of pairs of(a,b) such that index of a<index of b , we can do :-

for each “a” in original string , sum+= no. of b’s after its occurence * (k) + total no. of b’s in original string*( ((k-1)+(k-2)+…+1)=k*(k-1)/2);

sum will be the answer

for code , refer :- Div8Ty - Online C++0x Compiler & Debugging Tool - Ideone.com

3 Likes

@siddharthp538 -

If I am not wrong, then c is denoting the number of ‘ab’ sequences in original string.

Your example is very well correct. Your applied concept is correct for that category of test case. But again, that’s just one of the many categories.

You stated that " C here will act as both common diff as well as the first term." . This is wrong. This is a property of this particular string. The common difference actually is number of ‘b’ in the string, and since here number of ‘b’ is equal to number of ‘ab’, you see the equality.

Also, there are some tricky categories of test cases.
One of the categories of the test case are the ones with no ‘ab’ sequence in original string.

Consider string “ba” repeated twice, giving “baba”. Now your c is 0. So if your formula has a multiplication with c, it will give a result 0.

I ran your code for this test case-

Input
1
3 2
baa
Output
1

While I didn’t debug your code (too many variables, made it confusing to see which variable is doing what), I saw that your program prints 1 (if k=2) for strings like “baaa” “baaaaaaa” &etc. I think this will help you find the flaw in logic/code.

What I meant when I said there is an AP perspective, is that, look at the final string.

Let s=“aba” and k=3. Final string (say p) is p= “abaabaaba”

For the very first a in s. It is used to form 1 ab. Also, we can say that p is formed of “aba aba aba” (I inserted a space between repetitions intentionally for clarity). Now look at the starting a of all 3 “aba” and find number of ab subsequence using that particular ‘a’

For 'a' at index 0, ab seq= 3.
For 'a' at index 3, ab seq=2,
For 'a' at index 6, ab seq=1. 

I meant that this is an forming an AP. Similarly then I look at ‘ab’ s formed by every ‘a’ present in the string, and sum them up to get the final answer. See that it covers all ‘a’ and hence every possible ‘ab’ is covered, giving correct answer. Some observation and playing with cases/random samples would yield common difference= Number of b in string s, and a= number of ab formed in s by that particular a. Then we can safely apply the AP sum formula to find the answer.

1 Like

@pro1992

If x be the no. of sub-sequence of ab found in s then for each of the k strings would have x no. of sub-sequence ab. and it will contribute total of k*x in final answer.

coming to the second part. now we will consider if we chose ‘a’ from some string i and ‘b’ for some string j.

suppose we chose ‘a’ from the first string then we have cntA choice for picking up ‘a’ and cntB*(k-1) choice for picking up ‘b’ so total pairs of ‘ab’ would be cntA*[cntB*(k-1)] similarly if we choose ‘a’ from the second string then we will have cntB*(k-2) choice for picking ‘b’ and total ‘ab’ pairs in this case would be cntA*[cntB*(k-2)] and so on. The final equation will look like:

ans = cntA*cntB*(k-1)+cntA*cntB*(k-2)+cntA*cntB*(k-3)+…+cntA*cntB*1

Taking cntA*cntB common in above equation we get:

ans = cntA*cntB*[(k-1)+(k-2)+(k-3)+…+1]

where [(k-1)+(k-2)+(k-3)+…+1] is the sum of first (k-1) natural numbers which is given by [k(k-1)/2], so the answer become.

ans=cntA*cntB*[k(k-1)/2]

and then we add the case 1 so the final answer would be:

ans= (k*x)+(cntA*cntB*[k(k-1)/2])

If you are still in doubt then put in comments.

1 Like

Fixed formatting…again.

There is a big blunder in your code. Its HERE-

scanf("%d%d",&n,&k);

n and k aren’t int, they are usigned long long int. Due to wrong string/format specifier (i.e. “%d”) they, n and k were being assigned garbage values.

But still it is showing wrong answer.

I used the same. AP gave a very nice solution to me!

Give me time to debug.

correct answer is 5 for this test case not 3.

2 ‘ab’ pairs for first ‘b’ and 3 ‘ab’ pairs for second ‘b’.
You can verify by trying it in setter’s solution.

1 Like

Yes, i got that. My bad. This is what happens when compres keep you sleep deprived haha XD

Got AC for your code. You are suffering from overflow.

Changed-

int j=0,a= 0,b =0, sum = 0;

to

long long int j=0,a= 0,b =0, sum = 0;

Link- CodeChef: Practical coding for everyone

Reason for error- Overflow caused program to behave unpredictable. and gave error when judge ran it.

Thanks a lot bro…

If we repeat “aba” 2 times, it becomes abaaba. Your formula gives answer as-

Sum = 1 x (2 x 3/2)= 3. (If I get your approach correctly) and hence your code fails. Try to account of trailing 'a’s in the end.

@vijju123 got your point.