pku 2408 字符串的最小表示

题意很简单,给出一堆单词,如果在单词a,b在某种排列时相等,则a、b相等。
然后只要将单词最小表示就可以。
贴代码
 1 # include <cstdio>
 2 # include <cstring>
 3 # include <vector>
 4 # include <string>
 5 # include <algorithm>
 6 using namespace std;
 7 # include <map>
 8 map<string,vector<string> > refer;
 9 vector<pair<string,vector<string> > > ans;
10 void ssort(char *str)
11 {
12    int c[26],len=strlen(str);
13    memset(c,0,sizeof(c));
14    for(int i=0;i<len;i++)
15      c[str[i]-'a']++;
16    for(int i=0;i<26;i++)
17      while(c[i])
18      {
19        *str=i+'a';
20        c[i]--;
21        str++;
22      }
23 }
24 bool cmp(const pair<string,vector<string> > &a,const pair<string,vector<string> > &b)
25 {
26      if(a.second.size()!=b.second.size())
27        return a.second.size()>b.second.size();
28      else
29        return a.first<b.first;
30 }
31 int main()
32 {
33    // freopen("input.txt","r",stdin);
34    // freopen("output.txt","w",stdout);
35     char str[20];
36     while(scanf("%s",str)!=EOF)
37     {
38       string ori(str);
39       ssort(str);
40       refer[string(str)].push_back(ori);
41     }
42     for(map<string,vector<string> >::iterator it=refer.begin();it!=refer.end();it++)
43       ans.push_back(*it);
44     for(int i=0;i<ans.size();i++)
45     {
46         sort(ans[i].second.begin(),ans[i].second.end());
47         ans[i].first=ans[i].second[0];
48     }
49     sort(ans.begin(),ans.end(),cmp);
50     for(int i=0;i<5&&i<ans.size();i++)
51     {
52        vector<string>::iterator end=unique(ans[i].second.begin(),ans[i].second.end());
53        printf("Group of size %d: ",ans[i].second.size());
54        for(vector<string>::iterator it=ans[i].second.begin();it!=end;it++)
55            printf("%s ",it->c_str());
56        printf(".\n");
57     }
58     //system("pause");
59     return 0;
60 }
61 

posted on 2010-10-28 02:07 yzhw 阅读(84) 评论(0)  编辑 收藏 引用 所属分类: data struct


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


<2010年10月>
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

统计

公告

统计系统

留言簿(1)

随笔分类(227)

文章分类(2)

OJ

最新随笔

搜索

积分与排名

最新评论

阅读排行榜