-1

Usually the number of inputs is given and i can do it with a for loop but not there : https://codeforces.com/contest/469/problem/A

I am trying desesperately to construct a vector from a line of ints , but i just know than the number of ints cannot be higher than 100. There s 2 lines of inputs.

If i make an infinite loop of Cin , how can i break it when it reaches an end of line or the end of the inputs ?

Arghantyl
  • 3
  • 1
  • 1
    It looks to me that the number of input values *is* given: "*The next line contains an integer p (0 ≤ p ≤ n) at first, then follows p distinct integers .... The next line contains the levels Little Y can pass in the same format.*". So from that line you will have a vector of `p` integers. And there will be one more line of the same format. – Kevin Jul 07 '20 at 16:10
  • Oh my bad you are right. But is my tecnical question possible ? – Arghantyl Jul 07 '20 at 16:13
  • @Arghantyl yes, it is possible. Simply read the line into a `std::string` first, then assign that to a `std::istringstream`, and then read `int`s from it in a loop until there is nothing left to read. – Remy Lebeau Jul 07 '20 at 17:52

2 Answers2

0

You might consider reading in the entire line as a string first:

std::string line;
std::getline (istream,name);

then splitting that string into your vector (boost example):

std::vector<int> split(std::string const& str)
{
    using namespace std;
    using namespace boost;
    std::vector<int> results;
    tokenizer<> tok(str);
    for(tokenizer<>::iterator beg=tok.begin(); beg!=tok.end();++beg){
       results.emplace_back(std::stoi(*beg));
    }
    return results;
}
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
  • @Arghantyl A similar trick is to use [`std::istream_iterator`](https://en.cppreference.com/w/cpp/iterator/istream_iterator). [Usage example.](https://stackoverflow.com/a/4423401/4581301) – user4581301 Jul 07 '20 at 16:50
  • Or simply `std::istringstream`, eg: `std::vector split(std::string const& str) { std::vector results; std::istringstream iss(str); int num; while (iss >> num) results.push_back(num); } return results; }` – Remy Lebeau Jul 07 '20 at 17:53
0

Actually, if you read the question carefully, the number of integers on each line is given.

The next line contains an integer p (0 ≤ p ≤ n) at first, then follows p distinct integers a 1, a 2, ..., a p (1 ≤ a i ≤ n). These integers denote the indices of levels Little X can pass. The next line contains the levels Little Y can pass in the same format. It's assumed that levels are numbered from 1 to n.

p = Number of integers on each line.

Have a look at the following code which has Accepted code verdict on Codeforces:

#include<iostream> 
#include<set> 
#include<iterator> 


int main(){

    int N;
    std::cin>>N;

    std::set<int> uniqueNumbers;

    int t1, t2;
    int x;

    std::cin>>t1;
    for(int i=0;i<t1;i++){
        std::cin>>x;
        uniqueNumbers.insert(x);
    }
    
    std::cin>>t2;
    for(int i=0;i<t2;i++){
        std::cin>>x;
        uniqueNumbers.insert(x);
    }

    std::cout<< ((uniqueNumbers.size()==N) ? "I become the guy." : "Oh, my keyboard!");

    return 0;
}

Verdict:

enter image description here

Deepak Tatyaji Ahire
  • 4,883
  • 2
  • 13
  • 35
  • Thank you . How do you find other coders solutions so fast ? I tried a lot of times. – Arghantyl Jul 07 '20 at 16:30
  • I coded it just now. My username is aDEEPAK7. Have a look at the screenshot – Deepak Tatyaji Ahire Jul 07 '20 at 16:31
  • Oh ok . I thought you looked at someone account like i tried to do a lot of times :) It was not against you . – Arghantyl Jul 07 '20 at 16:35
  • Note: A `set` may be overkill for this problem and wind up being slower than a sieving algorithm. `n` cannot exceed 100, so an array of 101 bools (to keep the index of a bool in line with the origin 1 level numbers). When you read `n`, set 1 to `n` true. When you parse a level that can be beaten, set that level's bool to false. If there are any true values in the array, the game can't be completed. With the set you have many O(log(n)) look-ups and one O(1) test against the size of the `set` vs the array's many O(1) look-ups and 2 O(n) iterations (one for initializing, and one for test) – user4581301 Jul 07 '20 at 16:46