CHEALG Editorial

PROBLEM LINK:

Practice
Contest

Tester: Roman Bilyi
Editorialist: Hussain Kara Fallah

PROBLEM EXPLANATION

Saeed was teaching a string compression algorithm. This algorithm finds all maximal substrings which contains only one character repeated one or more times (a substring is maximal if it we cannot add one character to its left or right without breaking this property) and replaces each such substring by the string “cK”, where K is the length of the substring and c is the only character it contains. For example, “aabaaa” is compressed to “a2b1a3”.

Saeed wanted to check if the students understood the algorithm, so he wrote a string S on the board and asked the students if the algorithm is effective on S, i.e. if the string created by compressing S is strictly shorter than S. Help them answer this question.

DIFFICULTY:

Easy

CONSTRAINTS

1 \leq |N| \leq 10^6

EXPLANATION:

Now this is a straight forward implementation task. We need to perform the algorithm and check the length of the result.

First, let’s break our string into maximal contiguous substrings of identical characters. (a substring is maximal if we cannot extend it from left or right without breaking the identical characters property). We can do this by a simple loop.

Let’s keep 2 iterators. The first iterator always starts a new substring. Initially, it starts from the beginning of the string, now let’s move with our second iterator forward while we have identical characters (keeping their count as well). After we arrive at a new character, it means our substring is finished, and we move our first iterator to the character we stopped at (the new substring start) and we keep extracting substrings until our main string is exhausted.

Now for each processed substring, it would increase our compressed string length by F(substring\,length)+1 where F(x) is the length of integer x.

Let’s take an example “aabaaa”, it would be broken into “aa”,“b”,“aaa” so the length of compressed string would be F(2)+1+F(1)+1+F(3)+1=6.

After finishing we compare our compressed string length to the original and that’s it. Check implementation for better understanding.

Editorialist solution
#include <bits/stdc++.h>
using namespace std;
int main() {
    int T;
    cin >> T;
    while (T--) {
        string str;
        cin >> str;
        int ans = 0;
        for (int j = 0; j < str.size();) {
            int i , l = 0;
            for (i = j; i < str.size() && str[j] == str[i]; i++)
                ++l;
            while(l > 0){
                ans++;
                l /= 10;
            }
            ans++;
            j = i;
        }
        if (ans >= str.size()) puts("NO");
        else puts("YES");
    }
} 
Tester Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
    int T;
    cin >> T;
    while (T--) {
        string str;
        cin >> str;
        int ans = 0;
        for (int j = 0; j < str.size();) {
            int i , l = 0;
            for (i = j; i < str.size() && str[j] == str[i]; i++)
                ++l;
            while(l > 0){
                ans++;
                l /= 10;
            }
            ans++;
            j = i;
        }
        if (ans >= str.size()) puts("NO");
        else puts("YES");
    }
} 

Simple Python Solution
The extra character ‘P’ is used for padding.

t = int(input())
while(t>0):
    ts = input()
    ts += 'P'
    es = ""
    count = 1
    for i in range(len(ts)-1):
        if(ts[i] == ts[i+1]):
            count += 1
        elif(ts[i] != ts[i+1]):
            es += ts[i]
            es += str(count)
            count = 1

    if(len(es) < (len(ts) -1)):
        print("YES")
    else:
        print("NO")
    t -= 1
1 Like

https://www.codechef.com/viewsolution/28531011

Why is this code giving WA for the first subtask ??

aaaaaaaaaabcdefgh

This test case fails brother. I haven’t gone through your code though.

2 Likes

Isn’t the answer for the testcase YES ??

No - it’s one of the sample testcases, dude :slight_smile:

1 Like

https://pastebin.com/LRZPwzb4

Why WA for this ??

Consider the testcase:

1
aaacc
1 Like

while(t>0){
String s=br.readLine();
s+=’ ';
String res="";
int count=1;
for(int i=0;i<s.length()-1;i++){
if(s.charAt(i)==s.charAt(i+1))
count++;
else{
res+=s.charAt(i)+count;
count=1;
}
}
if(res.length()<(s.length()-1))
System.out.println(“YES”);
else
System.out.println(“NO”);
t–;

whats wrong here??

i am not getting where i am wrong. can any one please help me out of this problem.
my code :

#include <bits/stdc++.h>
using namespace std;

int main() {
long long int t,i,j,n,arr[26];

cin>>t;
while(t--)
{   string a,b="";
    for(i=0;i<26;i++)     
    arr[i]=0;
    cin>>a;
    for(i=0;i<a.length();i++)
    arr[a[i]-'a']+=1;
    
    for(i=0;i<26;i++)
    {
        if(arr[i]>0)
        {
          b=b+'i'+to_string(arr[i]);
          
        }
    }
    if(b.length()<a.length())
    cout<<"YES\n";
    else
    cout<<"NO\n";
}

// your code goes here
return 0;

}

Consider the testcase:

1
ccabbcabab
1 Like

bro isme jo a10 bn rh hga uski length 3 le ji rha hgi bcoz it is a string