SPOJ问题 - 这里出了什么问题?

Nic*_*ick 2 c++ java algorithm

我正在回答这个问题:http: //www.spoj.pl/problems/JAVAC/

我用C++编写了提交的代码.我在下面粘贴它.提交一直给出错误的答案.我找不到一个失败的测试用例.谁能帮我吗 ?

非常感谢.

#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)
  {
    cout<<translate(current)<<endl;
  }
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

zw3*_*324 5

我没有尝试过(从未使用过SPOJ),但是有可能存在其他无效格式吗?例如:

123asd

_asd

asd___a

我现在正在弄乱你的代码,看看我是否能让它运行起来.祝好运!

编辑:通过!尝试添加以下内容:

  1. 当第一个或最后一个字母为' _' 时检测错误

  2. 检测是否有连续_的(例如,' a__b')

  3. 检测其他字符(例如' adq21#')

这三个案例都应该是错误.干杯!