poj—2408—Anagram Groups

题目链接
做了好久,没有注意到的一点就是不仔细看样例,害我弄了这么长时间,下次一定注意,
看样例,
看样例,
看样例。
ac代码:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <math.h>

using namespace std;

struct world
{
    char s[40];
    char sed[40];
}str[30005];

struct node
{
    char s[40];
    int num;
    int start;
}Node[30005];

bool cmpw(world a,world b)
{
    if(strcmp(a.sed,b.sed)==0)
        return strcmp(a.s,b.s)<0;
    else
        return strcmp(a.sed,b.sed)<0;
}

bool cmpn(node a,node b)
{
    if(a.num==b.num)
        return strcmp(a.s,b.s)<0;

    else
        return a.num>b.num;
}

int main()
{

    int n=0;
    while(scanf("%s",str[n].s)!=EOF)
    {
        int len=strlen(str[n].s);
        strcpy(str[n].sed,str[n].s);
        sort(str[n].sed,str[n].sed+len);
        n++;
    }
    sort(str,str+n,cmpw);

  //  for(int i=0;i<n;i++)
    //    cout<<str[i].sed<<endl;

    int a=1;
    Node[0].start=0;
    Node[0].num=1;
    strcpy(Node[0].s,str[0].s);
    for(int i=1;i<n;i++)
    {
        if(strcmp(str[i].sed,str[i-1].sed)==0)
            Node[a-1].num++;
        else
        {
            strcpy(Node[a].s,str[i].s);
            Node[a].num=1;
            Node[a].start=i;
            a++;
        }
    }
    sort(Node,Node+a,cmpn);

 //   for(int i=0;i<a;i++)
   //     cout<<Node[i].num<<" "<<Node[i].s<<" "<<Node[i].start<<endl;

   // for(int i=0;i<n;i++)
    //    cout<<str[i].s<<endl;

    for(int i=0;i<5;i++)
    {
//        if(i>=n)
//            break;
        int t=Node[i].start;
//        cout<<t<<endl;
        printf("Group of size %d:",Node[i].num);
        if(i>=a)
        {
            printf("\n");
            break;
        }

        printf(" %s",str[t].s);
        for(int j=1;j<Node[i].num;j++)
        {
            if(strcmp(str[t+j].s,str[t+j-1].s)==0)
                continue;
            printf(" %s",str[t+j].s);
        }
        printf(" .\n");
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值