• 统计相似字符串


    http://acm.xidian.edu.cn/problem.php?cid=1028&pid=5

    题目描述

    给N个字符串,请将他们以相同的组成元素(即组成的元素种类相同,每种元素的个数也一样)来分类,分类后按照原本出现的顺序输出!

    输入

    多组数据,最多100组数据,每组最多N<5000个字符串,每个字符串长度最多|s|<=8,保证都是小写字母

    输出

    输出多行,每行为同一类别的字符串组

    样例输入

    6
    eat tea tan ate nat bat
    

    样例输出

    eat tea ate
    tan nat
    bat

    1、排序+离散化处理

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<cstdlib>
     5 using namespace std;
     6 struct node{
     7     char arr[9],brr[9];
     8     int num;
     9 }st[10000];
    10 struct nod{
    11     char aa[9];
    12     int s,t,num;
    13 }st2[10000];
    14 bool cmp(node a,node b){
    15     if(strcmp(a.brr,b.brr)==0) return a.num<b.num;
    16     else return strcmp(a.brr,b.brr)<0;
    17 }
    18 
    19 bool cmp2(nod a,nod b){
    20     return a.num<b.num;
    21 }
    22 
    23 int main(){
    24     int n;
    25     while(scanf("%d",&n)!=EOF){
    26         memset(st,0,sizeof(st));
    27         memset(st2,0,sizeof(st2));
    28         for(int i=0;i<n;i++){
    29             scanf("%s",st[i].arr);
    30             st[i].num=i;
    31             strcpy(st[i].brr,st[i].arr);
    32             sort(st[i].brr,st[i].brr+strlen(st[i].brr));
    33         }
    34         
    35         sort(st,st+n,cmp);
    36         
    37         
    38         int i,j;
    39         strcpy(st2[0].aa,st[0].brr);
    40         st2[0].num=st[0].num;
    41         st2[0].s=0;
    42         st2[0].t=0;
    43         
    44         for(i=1,j=0;i<n;i++){
    45             if(strcmp(st[i].brr,st2[j].aa)!=0){
    46                 j++;
    47                 st2[j].num=st[i].num;
    48                 st2[j].s=i;
    49                 st2[j].t=i;
    50                 strcpy(st2[j].aa,st[i].brr);
    51             }
    52             else{
    53                 st2[j].t++;
    54             }
    55         }
    56         
    57         sort(st2,st2+j+1,cmp2);
    58         for(int k=0;k<=j;k++){
    59             printf("%s",st[st2[k].s].arr);
    60             for(int t1=st2[k].s+1;t1<=st2[k].t;t1++){
    61                 printf(" %s",st[t1].arr);
    62             }
    63             printf("
    ");
    64         }
    65     }
    66     return 0;
    67 } 

    2、排序+二分

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<cstdlib>
     5 #include<string>
     6 using namespace std;
     7 struct node{
     8     char arr[9],brr[9];
     9     int num;
    10 }st[10000];
    11 struct nod{
    12     char aa[9];
    13     int num;
    14 }st2[10000];
    15 bool used[10000];
    16 int n;
    17 /*快排必须要有二级排序*/
    18 bool cmp(node a,node b){
    19     if(strcmp(a.brr,b.brr)<0) return a.num<b.num;
    20     else return strcmp(a.brr,b.brr)<0;
    21 }
    22 
    23 
    24 int search(int x){
    25     int l=0,r=n;
    26     while(l<r){
    27         int mid=(l+r)/2;
    28         if(strcmp(st[mid].brr,st2[x].aa)>=0) r=mid;
    29         else l=mid+1;
    30     }
    31     return r;
    32 }
    33 int main(){
    34     while(scanf("%d",&n)!=EOF){
    35         memset(st,0,sizeof(st));
    36         memset(st2,0,sizeof(st2));
    37         memset(used,0,sizeof(used));
    38         for(int i=0;i<n;i++){
    39             scanf("%s",st[i].arr);
    40             st2[i].num=st[i].num=i;
    41             strcpy(st[i].brr,st[i].arr);
    42             strcpy(st2[i].aa,st[i].arr);
    43             sort(st[i].brr,st[i].brr+strlen(st[i].brr));
    44             sort(st2[i].aa,st2[i].aa+strlen(st2[i].aa));
    45         }
    46         
    47         sort(st,st+n,cmp);
    48         
    49         
    50         for(int i=0;i<n;i++){
    51             int temp=search(i);
    52             if(used[temp]) continue;
    53             bool flag=false;
    54             while(true){
    55                 if(flag==false){ printf("%s",st[temp].arr);flag=true;}
    56                 else printf(" %s",st[temp].arr);
    57                 used[temp]=true;
    58                 if(strcmp(st[temp+1].brr,st[temp].brr)!=0){
    59                     printf("
    ");
    60                     break;
    61                 }
    62                 temp++;
    63             }
    64             
    65         }
    66     }
    67     return 0;
    68 } 
  • 相关阅读:
    黑马程序员:3分钟带你读懂C/C++学习路线
    大学毕业的你,满腔洪荒之力却找不到出口?
    从零基础到精通的前端学习路线
    Python学习笔记(2)——Python的函数、模块、包和库
    Python学习笔记(1)——Python的概述(Python的环境、变量、数据类型、基本运算)
    MATLAB优化——减少for的使用
    初识Python(windows)——下载、安装、使用
    高维数据稀疏表示-什么是字典学习(过完备词典)
    用1天快速上手org-mode(windows系统)
    零基础数据挖掘学习清单
  • 原文地址:https://www.cnblogs.com/elpsycongroo/p/6720124.html
Copyright © 2020-2023  润新知