Sunday, 10 September 2017

UVa Problem 483 - Word Scramble (C++)

Problem:

Please find the problem 
here.

Solution:

This is the simplest problem I ever had, just implement the given formulas! However, it does take me some time to write.

Code:


#include <string>
#include <iostream>

using namespace std;

int main(){

    string line;

    while(getline (cin,line)) {                          

                  int tam = line.size();//10 size of first line
                  int cont;
                  string sub;
                  for(int i=0;i<tam;i++){    //I
                       cont=0;

                       if(line[i]!=' '){   // If there is no space
                                while(line[i+cont]!=' ' && i+cont<tam){
                                       cont++; //count becomes 1
                                }
                                for(int j=(i+cont)-1;j>=i && j<tam;j--){ //print last word j then decrease j--
                                          cout<<line[j];
                                }
                                i+=cont-1; //the next word which is a space
                       }else{ //if there is space
                            cout<<line[i];
                       }
                  }
                  cout<<endl;
    }

 return 0;
}


No comments:

Post a Comment

Spoj Problem ACMCEG2C - Pick the candies (C++)

  The problem link may be found here.       Explanation: Use Deque to keep track of elements of the variety of candies. If i is gre...