SPOJ question - WHat is going wrong here?
I was answering this spoj question: http://www.spoj.pl/problems/JAVAC/
I coded the submission in C++. I am pasting it below. The submission consistently gives wrong answer. I cannot find a test case for which it will fail. Can anyone help me out ?
Thanks very much.
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
char *ans;
bool iscapital(char c)
{
if(c >='A' && c <='Z')
return true;
else
return false;
}
string translate(string current)
{
ans = new char[2*current.length() + 1];
int jflag = 0, cflag = 0,j = 0, i = 0;
if(iscapital(current[0]))
{
return "Error!";
}
while(i < current.length())
{
if(current[i] !='_')
{
if(!(current[i] >= 'A' && current[i] <= 'Z'))
{
ans[j] = current[i];
i++;
j++;
}
}
if(current[i] == '_')
{
if(jflag == 1)
return "Error!";
cflag = 1;
i++;
if(i < current.length())
{
//convert to capital
if(iscapital(current[i]))
return "Error!";
ans[j] = (char)((int)current[i] - 32);
i++;
j++;
}
}
if(iscapital(current[i]))
{
if(cflag == 1)
return "Error!";
jflag = 1;
ans[j] = '_';
j++;
ans[j] = (char)((int)current[i] + 32);
j++;
i++;
}
}
ans[j] = '\0';
string ret = ans;
return ret;
}
int main()
{
string current;
while(cin>>current)
{
c开发者_高级运维out<<translate(current)<<endl;
}
return 0;
}
I haven't tried it (never used SPOJ), but could it be possible that there are other invalid formats? E.g.:
123asd
_asd
asd___a
I am messing with your code now, see if I can make it works. Good luck!
EDIT: Passed! Try add the following:
Detect error when first or last letter is '
_
'Detect if there are consecutive '
_
's (e.g., 'a__b
')Detect other characters (e.g., '
adq21#
')
All the three cases here should be errors. Cheers!
精彩评论