reverse the strings...

I came over this question from google codeJam.
http://code.google.com/codejam/contest/351101/dashboard#s=p1

I was able to write the code.
But it was only working for small inputs, it was unable to work for large inputs.

Is their any enhancements that can be made to my code/logic.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  #include<iostream>
#include<string>
#include<cstring>
#include<stdio.h>
using namespace std;
int main(){
        freopen("input.txt", "rt", stdin);
        freopen("output.txt", "wt", stdout);
    int n,a=0;
    cin>>n;
    char que[1000];
    char *k;
    string ans[600];
    for(int i=1; i<=(n+1); ++i){
            //gets(buf);
            cin.getline(que,200);
            k=strtok(que," ");
            while(k!=NULL){

                ans[a].assign(k);
                k=strtok(NULL," ");
                a++;
            }
            cout<<"case #"<<i-1<<": ";
            for(int p=(a-1); p>=0;p--)
                cout<<ans[p]<<" ";
            cout<<endl;
            a=0;
    }
    return 0;
}
i would do it with vector.
push_back() each world of a line, untill you find '\n' or eof() then print from the end.


in your code, i think, you need to increase the capacity of line16
and in for(int i=1; i<=(n+1); ++i) seems you run one extra time!
Last edited on
Topic archived. No new replies allowed.