CHKSTR - Editorial

PROBLEM LINK :

contest

practice

Author : Vibhor Gupta

Tester : Vibhor Gupta

Editorialist : Vibhor Gupta

DIFFICULTY :

Medium-Hard

PREREQUISITES :

stringstream function

PROBLEM :

Rahul ha been given a string containing space separated integers. He does not know how many integers are in the string.

He has also been given 2 integers a,b. He has to make a fibonacci type of sequence with it. i.e nth term is sum of (n-1)th term and (n-2)th term.

Finally he has to calculate the sum of the nth integer in the above sequence for every integer n in the string.
As the output can be large, print the answer modulo 10^9 + 7

Flawed Code :

#include

#define gl(x) getline(cin,x);

using namespace std;

long long fib[1000000];

int main(){

int t;cin>>t;

while(tc–){

int a,b;cin>>a>>b;

fib[0]=a;fib1=b;

for(int i=2;i!=1000000;i++) fib[i] = (fib[i-1]+fib[i-2])%mod;

string s;cin>>s;

stringstream ss;

int x;

int sum=0;

while(ss>>x)sum += fib[x];

printf(“%d”,sum);}

EXPLANATION :

This question was quite simple, the main challenge was to take input of an unknown number of integers.

The normal cin could not be used as it would eat up the next test case also.

This problem was solved using the Stringstream function.

The corrected code can be view at, Corrected Solution

I got accepted by adding extra getline after reading a and b. Also by using cin.ignore(10000, ‘\n’) instead of getline.
Here’s my code: ideone CHKSTR
But i can’t understand why i get WA with ws manipulator. cin >> ws shoud do the same but gives WA. What’s wrong with ws?