乱序字符串的问题
来源:互联网 发布:君子知怕读后感 编辑:程序博客网 时间:2024/05/18 01:42
其实乱序字符串的实质是考察排序的问题,乱序字符串经过适当的转化就会变成一个排序问题,至于你使用什么排序方法就会有多种选择,插入,快速,堆。当然,如果采用建立HASH表的方式也是可以的,不过需要书写好的hash函数。
问题:
题目描述:给出一个字符串数组S,找到其中所有的乱序字符串(Anagram)。如果一个字符串是乱序字符串,那么他存在一个字母集合相同,但顺序不同的字符串也在S中。
样例:对于字符串数组 [“lint”,”intl”,”inlt”,”code”],返回 [“lint”,”inlt”,”intl”]
代码如下:
#include <stdlib.h>#include <string.h>#include <stdio.h>#define LEN 5#define MAXC 128 // 只针对ASCII码表的字符intComapareX(const void *e1, const void *e2){ return memcmp(*(int **)e1, *(int **)e2, MAXC * sizeof(int));}voidCalculateAllString(char ** a, int len){ int * X = (int *)malloc(sizeof(int) * len * MAXC); int i; int j; char * p; int ** X_INDEX_SORT = (int **)malloc(sizeof(int*) * len); memset(X, 0, sizeof(int) * len * MAXC); for (i=0; i<len; ++i) { *(X_INDEX_SORT+i) = X + MAXC * i; } // 使用字符统计方法,将所有的字符串序列的统计结果保存到X中。 for (i=0; i<len; ++i) { p = *(a+i); while (*p != '\0') { X[MAXC * i + *p] += 1; ++p; } } // 索引比较,这里使用对应的指针值 qsort(X_INDEX_SORT, len, sizeof(int*), ComapareX); j = (*(X_INDEX_SORT) - X) / MAXC; printf("%s ", a[j]); for (i=1; i<len; ++i) { j = (*(X_INDEX_SORT + i) - X) / MAXC; if (ComapareX(X_INDEX_SORT+i-1, X_INDEX_SORT+i) == 0) { printf("%s ", a[j]); } else { printf("\n%s ", a[j]); } } printf("\n"); free(X); free(X_INDEX_SORT);}intmain(int argc, char ** argv){ char * a[LEN] = {"linit", "intl", "inlt", "liint", "nlit"}; CalculateAllString(a, LEN); exit(0);}
0 0
- 乱序字符串的问题
- LintCode-乱序字符串
- 乱序字符串
- 题目:乱序字符串
- 乱序字符串
- LintCode 乱序字符串
- 乱序字符串
- (171)乱序字符串
- 乱序字符串_LintCode
- LintCode:乱序字符串
- 乱序字符串
- 乱序字符串算法
- 乱序字符串
- lintcode,乱序字符串
- 乱序字符串
- lintcode--乱序字符串
- 乱序字符串-LintCode
- 乱序字符串
- JAVA中Set集合
- PullToRefresh使用详解(一)--构建下拉刷新的listView
- AVL - 自平衡二叉树 - 详解
- Android开发使用的常见第三方框架汇总
- Fragment中处理触摸事件
- 乱序字符串的问题
- Anohana: The Flower We Saw That Day
- AllJoyn学习笔记一之认识AllJoyn
- Android开发中,那些让你相见恨晚的方法、类或接口
- 错误 sudo: unable to resolve host dev 及更改主机名
- My97时间输入控件,开始时间与结束时间的相互约束控制
- 插入排序----直接插入排序(C、C++、Python、Shell)
- Hbase集群搭建(一)
- 带你玩转Visual Studio——调用约定与(动态)库