高分求让我程序失败的测试用例(POJ 1226)

tonywearme 2013-04-09 09:49:22
大家帮我看下有什么测试用例会导致WA的吧,提交了好几次一直通不过,看了两天了。有时候自己的代码果然还是很难看出什么问题啊。

第一个给出测试用例让我代码失败的给100分!

链接:http://poj.org/problem?id=1226

Description
You are given a number of case-sensitive strings of alphabetic characters, find the largest string X, such that either X, or its inverse can be found as a substring of any of the given strings.

Input
The first line of the input contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case contains a single integer n (1 <= n <= 100), the number of given strings, followed by n lines, each representing one string of minimum length 1 and maximum length 100. There is no extra white space before and after a string.

Output
There should be one line per test case containing the length of the largest string found.

Sample Input

2
3
ABCD
BCDFF
BRCD
2
rose
orchid

Sample Output

2
2

我的代码如下,我想过的例子都通过了,但提交还是WA,通不过啊。

==========================================================================

#include <stdio.h>
#include <string.h>

const unsigned int kBufferSize = 105;
char g_shortest[kBufferSize];
char g_lines[100][210];

void reverse(char dest[], char source[])
{
unsigned int length = strlen(source);
for (int i = 0; i < length; ++i)
{
dest[i] = source[length - 1 - i];
}
dest[length] = '\0';
}

// lineNo starts from zero.
void scanLine(int lineNo)
{
char line[kBufferSize];
scanf("%s", line);

if ((strlen(g_shortest) == 0) || (strlen(line) < strlen(g_shortest)))
{
strcpy(g_shortest, line);
}

strcpy(g_lines[lineNo], line);
// Concatenate the reverse.
reverse(g_lines[lineNo] + strlen(line) - 1, line);
}

int getCommonLength(int lineNum)
{
int len = strlen(g_shortest);
while (len > 0)
{
// Enum all substrings of length "len" in "shortest",
// and test if that's contained by other strings.
//for (int i = 0; i < strlen(g_shortest); i += len
for (int i = 0; len <= strlen(g_shortest) - i; ++i)
{
char tmp = g_shortest[i + len];
// Mark the new(temp) end.
g_shortest[i + len] = '\0';

// Search all strings to see if this is a substring.
int j;
for (j = 0; j < lineNum; ++j)
{
if (!strstr(g_lines[j], g_shortest + i))
{
break;
}
}

// Restore the old.
g_shortest[i + len] = tmp;

if (j == lineNum)
{
return len;
}
}//for

--len;
}//while

// No common string found.
return 0;
}

int testCase()
{
g_shortest[0] = '\0';
int lineNum = 0;
scanf("%d", &lineNum);
for (int i = 0; i < lineNum; ++i)
{
scanLine(i);
}

return getCommonLength(lineNum);
}

int main()
{
int tests;
scanf("%d", &tests);

for (int i = 0; i < tests; ++i)
{
printf("%d\n", testCase());
}

return 0;
}
...全文
240 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
tonywearme 2013-04-09
  • 打赏
  • 举报
回复
引用 1 楼 liao05050075 的回复:
以前我们遇到这种情况,通常是写一个随机数据生成器,生成一堆的小数据,然后再跑跑。
谢谢!
tonywearme 2013-04-09
  • 打赏
  • 举报
回复
引用 3 楼 yunchao630 的回复:
引用 2 楼 yunchao630 的回复:1 2 123456789 78987应该输出3 实际输出5
哈哈,我刚才也发现了,AC了!果然大家都说如果3个小时都没发现还是第二天在看效率高。你说的没错,谢谢~ 100分。
翅膀又硬了 2013-04-09
  • 打赏
  • 举报
回复
引用 2 楼 yunchao630 的回复:
1 2 123456789 78987
应该输出3 实际输出5
翅膀又硬了 2013-04-09
  • 打赏
  • 举报
回复
1 2 123456789 78987
liao05050075 2013-04-09
  • 打赏
  • 举报
回复
以前我们遇到这种情况,通常是写一个随机数据生成器,生成一堆的小数据,然后再跑跑。

65,189

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧