Description
Autocomplete is a program function that enables inputting the text (in editors, command line shells, browsers etc.) completing the text by its inputted part. Vasya is busy working on a new browser called ‘BERowser’. He happens to be working on the autocomplete function in the address line at this very moment. A list consisting of n last visited by the user pages and the inputted part s are known. Your task is to complete s to make it an address of one of the pages from the list. You have to find the lexicographically smallest address having a prefix s.
Input
The first line contains the s line which is the inputted part. The second line contains an integer n (1 ≤ n ≤ 100) which is the number of visited pages. Then follow n lines which are the visited pages, one on each line. All the lines have lengths of from 1 to 100 symbols inclusively and consist of lowercase Latin letters only.
Output
If s is not the beginning of any of n addresses of the visited pages, print s. Otherwise, print the lexicographically minimal address of one of the visited pages starting from s.
The lexicographical order is the order of words in a dictionary. The lexicographical comparison of lines is realized by the ‘<’ operator in the modern programming languages.
Sample Input
Input
next
2
nextpermutation
nextelement
Output
nextelement
Input
find
4
find
findfirstof
findit
fand
Output
find
Input
find
4
fondfind
fondfirstof
fondit
fand
Output
find
题意:找出前缀是字符串s的,字典序最小的,如果没有输出s。
觉得应该是水题啊,但是WA好几次,在11组样例就错了,不知道哪里错了,希望大家帮忙找一下错误,这是我的WA代码
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct xx
{
char qq[101];
}x[101];
int cmp(const void *x, const void *y)
{
struct xx x1 = *(struct xx *)x;
struct xx y1 = *(struct xx *)y;
if(strcmp(x1.qq, y1.qq) > 0) return 1;
return 0;
}
int main()
{
char s[101];
int n;
while(~scanf("%s", s))
{
int ant = 0;
scanf("%d", &n);
for(int i = 0; i < n; i++)
scanf("%s", x[i].qq);
qsort(x, n, sizeof(x[0]), cmp);
for(int i = 0; i < n; i++)
{
if(strstr(x[i].qq, s) - x[i].qq == 0)
{
printf("%s\n", x[i].qq);
break;
}
else ant++;
}
if(ant == n) puts(s);
}
return 0;
}
大神们帮帮忙。