您的位置:首页 > 产品设计 > UI/UE

Uva 123 Searching Quickly

2013-10-28 12:13 330 查看
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=59

每输入一个句子,将不被忽略的词(not to ignore)及其所在位置保存下来,然后对所有这样的词排序,输出即可。

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

int n, m;
char ignored[55][15];
char s[205][1005];

int wn;
char words[2005][25];
int pr[2005];
int pc[2005];
int r[2005];

bool exist(char *str)
{
for (int i = 0; i < n; ++i) {
if (strcmp(str, ignored[i]) == 0) return true;
}
return false;
}

int icmp(const void *x, const void *y) {
return strcmp( words[*(int*)x], words[*(int*)y] );
}

int main()
{
n = 0;
while (1) {
gets(ignored
);
if (strcmp(ignored
, "::") == 0) break;
++n;
}
m = 0;
wn = 0;
while (gets(s[m]) != NULL) {
for (int i = 0; s[m][i]; ++i) s[m][i] = tolower(s[m][i]);
for (int i = strlen(s[m])-1; s[m][i]==' '; --i) s[m][i] = '\0';
char *p = s[m];
char tmp[25];
while (1) {
sscanf(p, "%s", tmp);
if (exist(tmp) == false) {
strcpy(words[wn], tmp);
pr[wn] = m;
pc[wn] = p-s[m];
++wn;
}
p += strlen(tmp);
while (*p == ' ') ++p;
if (!*p) break;
}
++m;
}
for (int i = 0; i < wn; ++i) r[i] = i;
qsort(r, wn, sizeof(r[0]), icmp);
for (int i = 0; i < wn; ++i) {
int k = r[i];
for (int j = 0; s[pr[k]][j]; ++j) {
if (j == pc[k]) {
for (int ii = 0; words[k][ii]; ++ii) {
putchar(toupper(words[k][ii]));
}
j += strlen(words[k])-1;
} else putchar(s[pr[k]][j]);
}
putchar('\n');
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: