TOPIC

time limit exceeded

DarKMasteR asked 2 years ago

can anyone tell me how can i solve the time limit exceed. and is there any easier method then this?

include

include

using namespace std;

main() { int n; cin >> n; getchar(); string t, ss[999],s[n];

for (int i = 0; i < n; i++)
{
    getline(cin, t);
    t.push_back(' ');
    int p = 0;
    for (int j = 0; j < t.length(); j++)
    {
        if (t[j] != ' ')
        {
            ss[p].push_back(t[j]);
        }
        else
        {
            ss[p].push_back(' ');
            p++;
        }
    }
    for (int j = 0; j < p; j++)
    {
        for (int k = j + 1; k < p; k++)
        {
            if (ss[j].length() < ss[k].length())
            {
                swap(ss[j], ss[k]);
            }
        }
    }

    for (int j = 0; j < p; j++)
    {
        cout<<ss[j];

    }
}

}

This topic was solved and cannot recieve new replies.

  • feodorv replied 2 years ago

    take a empty input

    "An empty input" in the folowing getline(cin, t);? In such case I use the code

    #include <iostream>
    #include <string>
    #include <sstream>
    
    int main()
    {
      string t;
      int n;
      getline( cin, t);
      istringstream(t) >> n;
      ...
    }

    So you can read input line by line.

  • feodorv replied 2 years ago

    cin >> n; getchar();

    You should not use simultaneously in one code the C functions and C++ operators for input reading. Better use

    cin >> n; cin >> t;
  • DarKMasteR replied 2 years ago

    Feodorv. i used getchar() to prevent auto input. when i used cin>>n without getchar compiler just take a empty input. you can run this code and check it your self