i trying http://www.spoj.com/problems/shlights/, have designed solution. new c++(about 14 days), , facing lot of problems. earlier used python, , there nothing of these errors, anyways, wrote this..
#include <iostream> #include <string> #include <cstdio> using namespace std; //example gbgbbb //t=0, gbgbbb t=1,bgbgbb t=2 bbgbgb, t=3 bbbgbg //search gb , replace bg //we need function replaces things string swapseq(string seq) { unsigned int sizeseq=seq.size(); unsigned int curr(0); while (curr<sizeseq-1) { if (seq[curr]=="g" , seq[curr+1]=="b") { seq[curr]="b";seq[curr+1]="g";curr+=2; } else {++curr;} } return seq; } int main() { unsigned int numcases; scanf("%d",&numcases); // cin>>numcases; (unsigned int currentcase=0;currentcase<numcases;++currentcase) { string seq; //scanf("%s",&seq); cin>>seq; string swapped=swapseq(seq); unsigned long long t=0; while (swapped!=seq) { swapped=swapseq(seq);++t; } printf("%lld\n",t); } return 0; } i know that's lot of details, that's it. spoj shows blank lines after inputs , outputs, after reading description, understand have things in single lines. here's g++4.7 compiler(linux)
shlights.cpp: in function ‘std::string swapseq(std::string)’: shlights.cpp:17:18: error: comparison string literal results in unspecified behaviour [-werror=address] shlights.cpp:17:18: error: iso c++ forbids comparison between pointer , integer [-fpermissive] shlights.cpp:17:37: error: comparison string literal results in unspecified behaviour [-werror=address] shlights.cpp:17:37: error: iso c++ forbids comparison between pointer , integer [-fpermissive] shlights.cpp:17:52: error: invalid conversion ‘const char*’ ‘char’ [-fpermissive] shlights.cpp:17:66: error: invalid conversion ‘const char*’ ‘char’ [-fpermissive] cc1plus: warnings being treated errors *what happening? there's pointers, const char , unspecified behaviour.
**i know pointers sort of variables point memory locations, nothing more.
**i've used scanf @ places , cin @ others(if replace scanf cin, same errors)
**is fact returned string took argument?
**where did use pointer?
**am wrong this- strings in c++ char arrays? if no, invalid conversion?
thanks in advance, , apologies wrong. if it's long, please answer of doubts.
you should use 'g' instead of "g" , on. when access char array (e.g. arr[5]) obtain char, can compare char literal (being: 'g') , not cstring (e.g. "g" or "google").
the compiler friend, points out problem is:
comparison string literal
Comments
Post a Comment