Ananagrams hoj 字符串模拟

136 篇文章 0 订阅
25 篇文章 0 订阅
/*一道比较水的字符串模拟。

刚开始的时候题意有点看错了。

就是注意在有重组可以相同的字符串的时候要把原有的字符串也删除掉。

这里用标记。

只要将每个字母重排就可以了。

还有大小写并没有区分在匹配的时候。*/

#include <iostream>

#include <stdio.h>

#include <cstring>
using namespace std;
char a[1001][21];
char b[1001][21];
int main()
{
    char s[22];
    char temp1[22];
    scanf("%s",s);
    int h=0;
    strcpy(a[h],s);//a作为原来的数组
    char c;
    for(int i=0; i<strlen(s); i++)
    {
        for(int j=i; j<strlen(s); j++)
        {
            if(s[i]>s[j])
            {
                c=s[i];
                s[i]=s[j];
                s[j]=c;
            }
        }
    }
    strcpy(b[h],s);
    h++;
    while(scanf("%s",s)==1)
    {
        if(s[0]=='#') break;
        strcpy(temp1,s);
        for(int i=0; i<strlen(s); i++)
        {
            if(s[i]>='A'&&s[i]<='Z')
                s[i]=s[i]+32;
        }
        for(int i=0; i<strlen(s); i++)
        {
            for(int j=i; j<strlen(s); j++)
            {
                if(s[i]>s[j])
                {
                    c=s[i];
                    s[i]=s[j];
                    s[j]=c;
                }
            }
        }
        bool ret=0;
        for(int i=0; i<=h-1; i++)
        {
            if(strcmp(s,b[i])==0)
            {
                ret=1;
                a[i][0]='*';
                break;
            }
        }
        if(ret==0)
        {
            strcpy(a[h],temp1);
            strcpy(b[h],s);
            h++;
        }
    }
    char te[22];
    for(int i=0; i<h; i++)
    {
        if(a[i][0]!='*')
        {
            for(int j=i; j<h; j++)
            {
                if(a[j][0]!='*')
                {
                    if(strcmp(a[i],a[j])>0)
                    {
                        strcpy(te,a[i]);
                        strcpy(a[i],a[j]);
                        strcpy(a[j],te);
                    }
                }
            }
        }
    }
    for(int i=0; i<h; i++)
    {
        if(a[i][0]!='*')
            printf("%s\n",a[i]);
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值