【编程珠玑】学习笔记[2]——变位词
来源:互联网 发布:网络信息安全概述 编辑:程序博客网 时间:2024/05/20 04:28
变位词:相同字母和字母数量,不同的组合顺序。如单词“stop”、“tops”就是变位词。
- 个人比较喜欢的解法——采用C++关联容器map
实现:
#include<iostream>
#include<map>
#include<iterator>
#include<string>
#include<fstream>
#include<algorithm>
using namespace std;
map<string,string> anagram;
//签名用到的比较函数,按升序排列
bool cmp(const char &x, const char &y)
{
return x<y;
}
//对词典中的单词进行签名,生成map对象
void sign(const string &infile)
{
ifstream in(infile);
if(!in)
{
cout <<"Unable to open the dictionary."<<endl;
}
string word;
while(in >> word)
{
string sig(word);
sort(sig.begin(), sig.end(), cmp); //签名
word[sig] += word; //添加到具有“sig”签名的单词集的值中
}
in.close();
}
void squash(const string &outfile)
{
ofstream out(outfile);
map<string,string>::iterator it = anagram.begin();
while(it != anagram.end())
{
out << it->second<<endl;
it++;
}
out.close();
}
int main(int argc, char **argv)
{
string infile;
string outfile;
cout << "Please input the filename of dictionary: "<<endl;
cin >>infile;
cin.clear();
cout << "Please input the filename for storing anagram: "<<endl;
cin >>outfile;
cin.clear();
sign(infile);
squash(outfile);
return 0;
}
- 编程珠玑上的解法——三段式“管道”结构
sign -> sort -> squash(其中sort采用系统的sort程序)。
//签名
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#define WORDMAX 100
int compare(const void *a,const void *b)
{
return *(char *)a - *(char *)b;
}
int main()
{
char word[WORDMAX], sig[WORDMAX];
while (scanf("%s", word) != EOF)
{
strcpy(sig, word);
qsort(sig, strlen(sig), sizeof(char), compare);
printf("%s %s\n", sig, word);
}
return 0;
}
//写入文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define WORDMAX 100
int main()
{
char word[WORDMAX], sig[WORDMAX], oldsig[WORDMAX];
int linenum = 0;
strcpy(oldsig, "");
while (scanf("%s %s", sig, word) != EOF)
{
if (strcmp(oldsig, sig) != 0 && linenum > 0)
{
printf("\n");
}
strcpy(oldsig, sig);
linenum++;
printf("%s ", word);
}
printf("\n");
return 0;
}
将生成的可执行文件放到同一个目录下,在命令行中用命令:sign <word.txt | sort | squash >anagram.txt 执行。其中,word.txt为要检测的字典, anagram.txt为排序后输出的,变位词在同一行。
附:关于sort和qsort函数
- sort
其中:
first为第一个元素位置;
last为最后一个元素位置;
comp为自定义比较函数(省略时,按升序进行排序)。
如对一个字符串(string str)进行排序:
sort(str.begin(), str.end(), comp)
bool comp(const char &x, const char &y)
{
retrun x<y; //升序
//return x>y; //降序
}
- qsort
其中:
base是待排序的一个集合;
num是这个数组元素的个数;
width是一个元素的大小(占用的字节数);
comp是一个自定义的比较函数。
比如:对一个长为1000的数组(int a[1000])进行排序, 那么base应为a,num应为 1000,width应为 sizeof(int),comp函数随自己的命名。即:qsort(a,1000,sizeof(int),comp);
其中comp函数可以为:
int comp(const void *a, const void *b)
{
return *(int *)a -*(int *)b;
}
上面是由小到大排序,若改为:return *(int *)b - *(int *)a; 为由大到小排序。
- 【编程珠玑】学习笔记[2]——变位词
- 《编程珠玑》学习总结2-变位词
- 编程珠玑——变位词
- 变位词--【编程珠玑】
- 编程珠玑变位词算法
- 编程珠玑--变位词问题
- 编程珠玑第二章变位词1
- [LeetCode] [编程珠玑:变位词]Anagrams
- 编程珠玑:变位词程序的实现
- 《编程珠玑》之变位词查找
- 变位词算法(编程珠玑第二章)
- 编程珠玑:变位词程序的实现
- 编程珠玑学习笔记
- 编程珠玑学习笔记
- 变位词实现 编程珠玑一处错误
- 编程珠玑--第二章变位词程序的实现
- 《编程珠玑》第二章2.8 变位词 思路
- 编程珠玑(二)变位词程序的实现p18
- Xms Xmx PermSize MaxPermSize 区别
- 地方规划法规和
- 的法国恢复国家法规和
- 13 PHP 回调函数
- 的国家法规和国会尽快
- 【编程珠玑】学习笔记[2]——变位词
- 对方国家法规和客户感觉
- 的法国恢复规划局法规环境
- 【数据结构】第八周研讨题
- JAVA学习方向(原文:http://blog.sina.com.cn/s/blog_9671d5180101lj4q.html)
- 路径的分割与合并
- 从eNSP 模拟器三层交换机不支持策略路由看模拟器的基本要求
- Jsp+servlet乱码处理的几种方式
- 1583个mms电台列表