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

时间:2011-06-24 16:54:53

标签: java c++ 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;
}

1 个答案:

答案 0 :(得分:5)

我没有尝试过(从未使用过SPOJ),但有可能还有其他无效格式吗? E.g:

123asd

_asd

asd___a

我现在正在搞乱你的代码,看看我是否可以使它运行起来。祝你好运!

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

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

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

  3. 检测其他字符(例如,“adq21#”)

  4. 这里的所有三个案例都应该是错误。干杯!