You are not logged in. Please login at www.codechef.com to post your questions!

×

ANY HELP WOULD B APPRECIATED?

This is a program to calculate the number of vowels in all the substrings of a given string for a number of test cases.

MY CODE:

include<iostream> //NOTE that for string "baceb" it gives answer 10 but shud give 16.

include<string>

using namespace std;

static int count=0;

inline void count1(string s)

{

for(int i=0;i<s.size();i++)

{

    if(s.at(i)=='a' ||s.at(i)=='e' ||s.at(i)=='i' ||s.at(i)=='o' ||s.at(i)=='u' || s.at(i)=='A'

||s.at(i)=='E' ||s.at(i)=='I' ||s.at(i)=='O' ||s.at(i)=='U')

    {count++;}

}

}

int main()

{

int t;

cin>>t;

string s[t];

for(int i=0;i<t;i++)

{

    cin>>s[i];


}

for(int i=0;i<t;i++)

{

    for(int j=0;j<s[i].size();j++)

    {

        for(int k=1;k<=s[i].size()-1-j;k++)

       {count1(s[i].substr(j,k));}

    }

    cout<<endl<<count;

    count=0;

}


return 0;

}

asked 12 Dec '18, 16:35

chaitu12789's gravatar image

0★chaitu12789
122
accept rate: 0%

edited 12 Dec '18, 16:37


Hi, @chaitu17789 --

Your problem is in this segment of code, in particular in the inner loop.

       for(int j=0;j < s[i].size();j++)
        {
            for(int k=1; k <= s[i].size()-1-j; k++)
            {
                count1(s[i].substr(j,k));
            }
        }

For example, if j (string starting position) is currently 0 and size() returns 5, I believe you want k (substring length) to range from 1 to 5. This is because when starting at position 0, you can have substrings of all lengths up to size().

To further the example, let s[i] be baceb. Starting from 0, possible substrings: b, ba, bac, bace, baceb

But your inner loop does not consider all possible substrings starting from that position.

I hope this helps!

link

answered 12 Dec '18, 20:05

sbatten1969's gravatar image

3★sbatten1969
583
accept rate: 16%

Thanks for ur help:)

(14 Dec '18, 17:42) chaitu127890★

Just remove "-1" from k<=s[i].size()-1-j and you are good to go.

You weren't checking all the substrings.

link

answered 12 Dec '18, 20:13

black_truce's gravatar image

5★black_truce
1276
accept rate: 27%

1

Thnaku i got my mistake:)

(14 Dec '18, 17:42) chaitu127890★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×630
×23

question asked: 12 Dec '18, 16:35

question was seen: 115 times

last updated: 14 Dec '18, 17:42