posts - 195,  comments - 30,  trackbacks - 0
2152: A Special Sorting
Status In/Out TIME Limit MEMORY Limit Submit Times Solved Users JUDGE TYPE
stdin/stdout 3s 8192K 356 152 Standard

Give you a sequence of words, can you sort them in dictionary order?

eg.

yes
accidental
baby
accept
accident
In dictionary order, those words should be printed as the following
accept
accident
accidental
baby
yes
But we will make an interesting rule this time. See your keyboard or the picture below:

there are ONLY 26 letters in lower-case we will use, and we assume that the letter above is bigger than the letter below, and the left is bigger than the right. For example, 'q' is bigger than 'a', 'c' is bigger than 'v'. So the example at the beginning whill be...

Input and Output

the input will only contain a sequence of words which is less than 1500. the output should print the sorted words each in a single line.

Sample Input

yes
accidental
baby
accept
accident

Sample Output

baby
accident
accidental
accept
yes

水题啊!
#include<iostream>
#include<cstdlib>
#include<string>
#include<algorithm>
using namespace std;
int Map[27];
struct M{
 char s[30];
}S[1500];
char table[26]={'q','w','e','r','t','y','u','i','o','p','a','s','d',
'f','g','h','j','k','l','z','x','c','v','b','n','m'};
bool operator<(M a,M b)
{
 int i=strlen(a.s);
 int j=strlen(b.s);
 if(i<j)
 j=i;
 for(int k=0;k<j;k++)
 {
  if(Map[(a.s[k])-'a']<Map[(b.s)[k]-'a'])
  return false;
  if(Map[(a.s[k])-'a']>Map[(b.s)[k]-'a'])
  return true;
 }
 return i==j;//Èç¹ûabcd,abc,abcd<abc
}
void search(char key)
{
 int i;
for(i=0;i<26;i++)
{
 if(key==table[i])
 {
  Map[key-'a']=i;
  break;
 }
}
}
  int main()
  {
  freopen("s.txt","r",stdin);  
  freopen("key.txt","w",stdout);
  int i;
  string t;
  memset(S,0,sizeof(S));
  for(i=0;i<26;i++)
  {
  search('a'+i);
  }
  i=0;
  while(!cin.eof())
   { 
 cin.getline(S[i++].s,30);//不能用cin>>s;否则runtime  error
   }
 sort(S,S+i-1);
 for(int j=0;j<i-1;j++)//必须用i-1,最后还有个空行。
 cout<<S[j].s<<endl;
  //system("PAUSE");
  return   0;
  }
奇怪的是在vc上能用
string s ;
while(cin>>s)
{};
在dev c++ 上不行。
后来才发现可以
string s ;
while(cin>>s)
{};
但是不能加 memset(S,0,sizeof(S));
posted on 2009-07-18 16:11 luis 阅读(626) 评论(0)  编辑 收藏 引用 所属分类: 格式.输入输出.数据类型

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿(3)

随笔分类

随笔档案

文章分类

文章档案

友情链接

搜索

  •  

最新评论

阅读排行榜

评论排行榜