problem 1181
来源:互联网 发布:linux下查看mysql用户 编辑:程序博客网 时间:2024/05/17 07:59
很笨的方法,排序然后折半。
Accepted 1181 C++ 00:00.00 436K#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct s
{
char w[7];
char sorted[7];
}word;
int search(word* a,int p,int r,char key[])
{
while(p <= r)
{
int q = (p + r) / 2;
int flag = strcmp(a[q].sorted,key);
if(flag == 0)
return q;
else if(flag > 0)
r = q - 1;
else p = q + 1;
}
return -1;
}
int cmp(const void* a, const void* b)
{
return *(char*)a - *(char*)b;
}
int cmp1(const void* a, const void* b)
{
int flag = strcmp((*(word*)a).sorted,(*(word*)b).sorted);
if(flag != 0) return flag;
else return strcmp((*(word*)a).w,(*(word*)b).w);
}
void solve(char list[][7],int cnt)
{
word w[100];
memset(w,0,cnt * sizeof(word));
for(int i = 0; i < cnt; i++)
{
strcpy(w[i].sorted,list[i]);
strcpy(w[i].w,list[i]);
qsort(w[i].sorted,strlen(w[i].sorted),sizeof(char),cmp);
}
qsort(w,cnt,sizeof(word),cmp1);
char str[7];
while(scanf("%s",str) != EOF && strcmp(str,"XXXXXX") != 0)
{
qsort(str,strlen(str),sizeof(char),cmp);
int temp = search(w,0,cnt - 1,str);
if(temp == -1) puts("NOT A VALID WORD");
else
{
while(strcmp(w[temp - 1].sorted,str) == 0) temp--;
while(strcmp(w[temp].sorted,str) == 0)
puts(w[temp++].w);
}
puts("******");
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1181.txt","r",stdin);
#endif
char list[101][7];
int cnt = 0;
while(scanf("%s",list[cnt]) != EOF)
{
if(strcmp(list[cnt++],"XXXXXX") == 0)
{
solve(list,cnt - 1);
memset(list,0,sizeof(list));
}
}
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0;
}
#include<string.h>
#include<stdlib.h>
typedef struct s
{
char w[7];
char sorted[7];
}word;
int search(word* a,int p,int r,char key[])
{
while(p <= r)
{
int q = (p + r) / 2;
int flag = strcmp(a[q].sorted,key);
if(flag == 0)
return q;
else if(flag > 0)
r = q - 1;
else p = q + 1;
}
return -1;
}
int cmp(const void* a, const void* b)
{
return *(char*)a - *(char*)b;
}
int cmp1(const void* a, const void* b)
{
int flag = strcmp((*(word*)a).sorted,(*(word*)b).sorted);
if(flag != 0) return flag;
else return strcmp((*(word*)a).w,(*(word*)b).w);
}
void solve(char list[][7],int cnt)
{
word w[100];
memset(w,0,cnt * sizeof(word));
for(int i = 0; i < cnt; i++)
{
strcpy(w[i].sorted,list[i]);
strcpy(w[i].w,list[i]);
qsort(w[i].sorted,strlen(w[i].sorted),sizeof(char),cmp);
}
qsort(w,cnt,sizeof(word),cmp1);
char str[7];
while(scanf("%s",str) != EOF && strcmp(str,"XXXXXX") != 0)
{
qsort(str,strlen(str),sizeof(char),cmp);
int temp = search(w,0,cnt - 1,str);
if(temp == -1) puts("NOT A VALID WORD");
else
{
while(strcmp(w[temp - 1].sorted,str) == 0) temp--;
while(strcmp(w[temp].sorted,str) == 0)
puts(w[temp++].w);
}
puts("******");
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1181.txt","r",stdin);
#endif
char list[101][7];
int cnt = 0;
while(scanf("%s",list[cnt]) != EOF)
{
if(strcmp(list[cnt++],"XXXXXX") == 0)
{
solve(list,cnt - 1);
memset(list,0,sizeof(list));
}
}
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0;
}
- problem 1181
- problem
- Problem
- problem
- Problem
- Problem
- Problem
- Problem
- Problem
- Problem
- problem
- Problem
- Problem
- Problem
- Problem
- Problem
- Problem
- Problem
- CodeGuru: Adding Your Logo to Winlogon??s Dialog
- DataGridView中的行如何根据不同的值显示不同的行背景色
- 好久没有来到这里了.
- 有点迷茫
- 简单代理服务器C代码实现(SOLARIS)
- problem 1181
- Eclipse 3.2 上配置JSP开发环境
- 远离电脑病完全手册
- 当重新安装Windows XP时保留OEM预激活
- JFreeChart 心得
- SEH&&try catch
- AJAX 介绍
- 搜到 一个js判断上传的文件大小的代码
- 验证码