• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Hug_Sea
博客园    首页    新随笔    联系   管理    订阅  订阅

2682 Problem H: New Feature in Windows 7

题意:给出n个字符串,按要求排序,要求如下:

If two filenames are same before certain position including start, the
order should be decided by these conditions:
a) If both of them are alphabets, the result depends on the alphabet order.
b) If one is alphabet and the other is number, the number is smaller.
c) If both of them are numbers, the result depends on number values.

例:

思路:学习了某位大神的sort排序..........

 

View Code
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <string>
 5 #include <algorithm>
 6 #include <iostream>
 7 using namespace std;
 8 const int N=110;
 9 
10 string str[N];
11 
12 int dig(char c){return c>='0'&&c<='9';}
13 int ler(char c){return !dig(c);}
14 
15 int cmp(string a,string b){
16     int i,j,len_a,len_b;
17     for(i=0;a[i]&&b[i];i++){
18         if(dig(a[i])&&dig(b[i])){
19             for(len_a=0,j=i;dig(a[j]);j++) len_a++;
20             for(len_b=0,j=i;dig(b[j]);j++) len_b++;
21             if(len_a<len_b) return 1;
22             else if(len_a>len_b) return 0;
23             else{
24                 for(int k=0;k<len_a;k++)
25                     if(a[i+k]!=b[i+k]) return a[i+k]<b[i+k];
26             }
27             i=j;
28         }
29         else if(dig(a[i])&&ler(b[i])) return 1;
30         else if(ler(a[i])&&dig(b[i])) return 0;
31         else{
32             if(a[i]!=b[i]) return a[i]<b[i]; 
33         }
34     }
35     if(a[i]) return 0;
36     return 1;
37 }
38 
39 int main(){
40     
41 //    freopen("data.in","r",stdin);
42 //    freopen("data.out","w",stdout);
43     
44     int n;
45     while(scanf("%d",&n),n){
46         for(int i=0;i<n;i++) cin>>str[i];
47         sort(str,str+n,cmp);
48         for(int i=0;i<n;i++) cout<<str[i]<<endl;
49         cout<<endl;
50     }
51     return 0;
52 }

 

 

 

posted @ 2012-05-09 15:12  Hug_Sea  阅读(101)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3