Strings

Hi,i'm given a number and a string.I have to check if i can separate the string in pieces equal to the number and if i can i print yes or no.To separate a string the start of the new it must be different from the starting letter of the previous string.This is my code and i have a problem on a test that it's given number 2 and string aaacas and the correct output is YES aaa cas. My code prints Yes aa acas.
Thank you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<iostream>
 #include<string>
 using namespace std;
 int main(){
 bool Alphabet[26];
 string A;
 int N,i,counter;
 char a;

 cin>>N;
 cin>>A;

 counter=0;
 a=A[0];
 Alphabet[A[0]-97]=false;
 for(i=1;i<A.size();i++){
    if(N==1)
        break;
    if(A[i]!=a && Alphabet[A[i]-97]!=true){
            counter++;
            a=A[i];
            Alphabet[A[i]-97]=true;
    }
    else if(A[i]==a){
    Alphabet[A[i]-97]=false;
    a=A[i];
    }

 }
if(N==1){
    cout<<"YES"<<endl;
    cout<<A;
}
else if(counter>=N){
        cout<<"YES"<<endl;
    for(i=0;i<N;i++){
        for(i=0;i<A.size();i++){
            if(Alphabet[i]==false)
                cout<<A[i];
            else if(Alphabet[i]==true){
                cout<<endl;
                cout<<A[i];
            }
        }
    }
}
else{
    cout<<"NO"<<endl;
    cout<<A;
}

}
First of all, your code is much longer than it should be. It uses 48 lines of code when it should really use around 30 (not counting empty lines). Not really sure why you were storing every single character in the alphabet in an array. Here's a very simplified version that works:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>

using namespace std;

int main (int argc, char** argv) //These values aren't used in this program,
//the reason they are here is because my editor automatically inserts the
//template which has these command-line parameters. Ignore these.
{
    string A;
    size_t N;
    unsigned int counter = 0; //Always have this initialized, especially if it is a counter variable.

    cin >> N;
    cin >> A;

    cin.clear(); //These two calls are to 
    cin.ignore();//clear the error flags since it already has an object.



     if(A.size() % N == 0)
     {
         cout << "Yes:" << endl;

         for (const auto& i : A) 
                       //This is a range-based for loop, available since the C++11 standard. 
                       //If your compiler doesn't support this, use a nested for loop instead. 
         {
             cout << i;
             counter++;

             if (counter == (A.size() / N))
             {
                 counter = 0;
                 cout << endl;
             }
         }

     } else {

         cout << "No";

     }

    return 0;
}



You're welcome :)

EDIT:
Also, your code is very hard to read. You should space your code lines out a little bit and add spaces between conditionals, preprocessor directives, and statements to improve the legibility. From your code:

1
2
3
4
5
6
7
8
9
#include<iostream>
 #include<string>
 using namespace std;
 int main(){
 bool Alphabet[26];
 string A;
 int N,i,counter;
 char a;
...


vs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
 #include<string>

 using namespace std;

 int main() 
{

   bool Alphabet[26];
   string A;

   int N, i, counter;
    char a;
...
Last edited on
Thank you so much!!!! It works!! I will follow your advice about my codes :)
There are some errors with some test cases but i will fix them
Topic archived. No new replies allowed.