uva 156 Ananagrams(字符串模拟搜索)

题意是找到一个字符串里面所有可以重排的单词,最后字典序输出。


把所有单词找出来,全换成小写,并将字母排序。

搜索排序后一样的单词,标记。

最后没上标记的字典序输出就行了。


脑袋糊糊的,写的乱七八糟的。


代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int Maxn = 1010;

struct String
{
    char str[100];
    char low[100];
    bool no;//记录单词是否存在
} s[Maxn];

struct Ans//保存结果
{
    char str[100];
} a[Maxn];

//字典序排序
bool cmp(Ans a, Ans b)
{
    return strcmp(a.str, b.str) < 0;
}

int main()
{
    #ifdef LOCAL
    freopen("in.txt", "r", stdin);
    #endif // LOCAL
    int cnt = 0;
    while (scanf("%s", s[cnt].str) != EOF)
    {
        if (s[cnt].str[0] == '#')
            break;
        //将大写转换成小写
        for (int i = 0; i < strlen(s[cnt].str); i++)
        {
            if (s[cnt].str[i] <= 'Z' && s[cnt].str[i] >= 'A')
                s[cnt].low[i] = s[cnt].str[i] + 32;
            else
                s[cnt].low[i] = s[cnt].str[i];
        }
        //将输入的单词字母排序,排序后单词存在low中,原单词在str中
        sort(s[cnt].low, s[cnt].low + strlen(s[cnt].str));
        //标记为字符串中未出现
        s[cnt++].no = true;
    }
    //搜索
    for (int i = 0; i < cnt; i++)
    {
        for (int j = i + 1; j < cnt; j++)
        {
            //若存在重排单词 即 单词出现
            if (strcmp(s[i].low, s[j].low) == 0)
            {
                s[i].no = false;
                s[j].no = false;
            }
        }
    }
    int n = 0;
    for (int i = 0; i < cnt; i++)
    {
        //若单词未出现
        if (s[i].no)
        {
            strcpy(a[n++].str, s[i].str);
        }
    }
    //字典序
    sort(a, a + n, cmp);
    for (int i = 0; i < n; i++)
        printf("%s\n", a[i].str);
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值