POJ2503的代码,在自己编译器上实验无数遍是正确的,为什么一提交就是WA呢?我有遗漏了什么吗?球球急回答
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
const int MAX = 100001;
struct dictionary
{
char word1[11];
char word2[11];
}dict[MAX];
int quickpass(dictionary dict[], int low, int high)
{
int down = low, up = high;
dictionary x = dict[down];
while (down < up)
{
while (down < up && strcmp(dict[up].word2, x.word2) > 0)
up--;
if (down < up) dict[down++] = dict[up];
while (down < up && strcmp(dict[down].word2, x.word2) < 0)
down++;
if (down < up) dict[up--] = dict[down];
}
dict[down] = x;
return down;
}
void quicksort(dictionary dict[], int low, int high)
{
int mid;
if (low < high)
{
mid = quickpass(dict, low, high);
quickpass(dict, low, mid - 1);
quickpass(dict, mid + 1, high);
}
}
int binarysearch(char str[], int num)
{
int low = 0, high = num - 1;
int mid, judge;
while (low <= high)
{
mid = (low + high) / 2;
judge = strcmp(str, dict[mid].word2);
if (judge == 0) return mid;
else if (judge == -1) high = mid - 1;
else low = mid + 1;
}
return -1;
}
int main()
{
char str[255], str1[25], str2[25];
int pos1 = 0, pos2 = 0, num = 0;
while (cin.getline(str, 255))
{
if (str[0] == '\0') break;
pos1 = 0; pos2 = 0;
while (str[pos1] != ' ')
str1[pos1] = str[pos1++];
str1[pos1] = '\0';
pos1++;
while (str[pos1] != '\0')
str2[pos2++] = str[pos1++];
str2[pos2] = '\0';
strcpy_s(dict[num].word1, strlen(str1) + 1, str1);
strcpy_s(dict[num].word2, strlen(str2) + 1, str2);
num++;
}
quicksort(dict, 0, num - 1);
while (gets_s(str))
{
pos1 = binarysearch(str, num);
if (pos1 == -1) cout << "eh" << endl;
else cout << dict[pos1].word1 << endl;
}
return 0;
}