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

uva 123 - Searching Quickly

2013-08-13 12:50 435 查看
题意:给几个可以忽略的单词    然后给一些句子    句子中除了忽略的单词都是关键字     对关键字排字典序      对同一个关键字 先输入的先输出  同一个句子中两个关键字相同的关键字在前面的排前面      最后输出的时候除了关键字大写  其余全是小写

下面是代码

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;

int T,cnt;

struct STR
{
char s[1000];
int n;
int l;
int visit;
}str[205],word[3005],result[3005];

int Compare(struct STR a, struct STR b)
{
if(a.l < b.l)return 1;
if(a.l > b.l)return 0;
if(a.visit < b.visit)return 1;
if(a.visit > b.visit) return 0;
}

int cmp(struct STR a, struct STR b)
{
int i;
if(strcmp(a.s,b.s) == 0)
return Compare(result[a.n],result[b.n]);
else
{
for(i = 0; a.s[i]!='\0'&&b.s[i]!='\0'; i++)
{
if(a.s[i] < b.s[i])return 1;
else if(a.s[i] > b.s[i])return 0;
}
if(a.s[i] == '\0')return 1;
else if(b.s[i] == '\0')return 0;
}
}

int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
char s[55][1005];
int n = 0;
while(gets(s
))
{
if(strcmp(s
,"::")==0)break;
n++;
}
while(gets(str[T].s)!=NULL)
{
for(int j = 0; str[T].s[j]!='\0';j++)
if(isalpha(str[T].s[j]))str[T].s[j] = tolower(str[T].s[j]);
T++;
}
for(int i = 0; i < T; i++)
{
for(int j = 0; str[i].s[j]!='\0'; j++)
{
if(!isalpha(str[i].s[j]))continue;
int t = 0;
word[cnt].visit = result[cnt].visit = j;
while(isalpha(str[i].s[j]))word[cnt].s[t++] = str[i].s[j++];
word[cnt].s[t] = '\0';
int flag = 1;
for(int k = 0; k < n&&flag; k++)
if(strcmp(word[cnt].s,s[k]) == 0){flag =0;break;}
if(flag)
{
word[cnt].n = cnt;
result[cnt].l = word[cnt].l = i;
strcpy(result[cnt].s,str[i].s);
for(int x =0 ; word[cnt].s[x]!='\0';x++)
{
word[cnt].s[x] = toupper(word[cnt].s[x]);
result[cnt].s[word[cnt].visit+x] = word[cnt].s[x];
}
cnt++;
}
}
}
sort(word,word+cnt,cmp);
for(int i = 0 ; i < cnt; i++)
puts(result[word[i].n].s);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: