《编程珠玑》代码之路4:变位词集合完整代码

字典的变位词集合:变位词就是由相同的字母的不同顺序组成的单词,例如pots和stop就是变位词,按构成字母顺序排序的opst就是他两的标志。

现在给一个字典,把变位词一起输出:

例如一个字典:

pans
pots
opt
snap
stop
tops

那么对应的标志分别是:

anps pans
opst pots
opt opt
anps snap
opst stop
opst tops

最后输出:

anps: pans snap 
opst: pots stop tops 
opt: opt 

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

class Word{
public:
	string word;//单词
	string sign;//标志

	Word(){
		word = "";
		sign = "";
	}

	bool operator < (const Word &b) const {
		return sign < b.sign;
	}
}dict[10000];

int nWord = 0;

//读取数据
int readData(Word dict[]);
//计算每个单词的标志sign
int Sign(Word dict[]);
//按标志sign给字典排序
int Sort(Word dict[]);
//按照标志输出单词
int Squash(Word dict[]);

int main(){
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);

	readData(dict);
	Sign(dict);
	Sort(dict);
	Squash(dict);

	return 0;
}

int readData(Word dict[]){
	char word[100];
	
	while (scanf("%s", &word) != EOF){
		dict[nWord].word = dict[nWord].sign = word;
		cout << dict[nWord].word << endl;
		nWord++;
	}
	return 0;
}

int Sign(Word dict[]){

	freopen("sign.txt", "w", stdout);
	for (int i = 0; i < nWord; ++i){
		sort(dict[i].sign.begin(), dict[i].sign.end());
		cout << dict[i].sign << ' ' << dict[i].word << endl;
	}
	return 0;
}

int Sort(Word dict[]){
	freopen("sort.txt", "w", stdout);
	sort(dict, dict + nWord);

	for (int i = 0; i < nWord; ++i){
		cout << dict[i].sign << ' ' << dict[i].word << endl;
	}
	return 0;
}

int Squash(Word dict[]){
	freopen("out.txt", "w", stdout);

	string oldSign = "";
	int curWord = 0;

	cout << dict[0].sign << ": ";
	for (int i = 0; i < nWord; ++i){
		if (oldSign != dict[i].sign && curWord > 0){
			cout << endl;
			cout << dict[i].sign << ": ";
		}

		curWord++;
		oldSign = dict[i].sign;
		cout << dict[i].word << ' ';
	}

	return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Code from Programming Pearls Column 1: Programs for sorting integers bitsort.c -- Sort with bit vectors. sortints.cpp -- Sort using C++ STL sets. qsortints.c -- Sort with C library qsort. bitsortgen.c -- Generate random integers for sorting. Column 2: Test and time algorithms rotate.c -- Three ways to rotate the elements of a vector. The next two program are used in a pipeline to compute all anagrams in a dictionary sign.c -- Sign each word by its letters in sorted order. squash.c -- Put each anagram class on a single line. Column 5: Scaffolding for testing and timing search functions search.c -- Linear and binary search. Column 7: Tiny experiment on C run times timemod0.c -- Edit main to time one operation. Column 8: Compute the maximum-sum subsequence in an array maxsum.c -- Time four algs: n3, n2, n log n, n. Column 9: Code tuning programs genbins.c -- Profile this, then try a special-purpose allocator. macfun.c -- Time the cost of macros and functions. The column also uses rotate.c (Column 2), search.c (Column 5) and maxsum.c (Column 8). Column 11: Test and time sorting algorithms sort.cpp -- Mostly C, but also C++ sort function. SortAnim.java -- Animate those sort functions in Java. Column 12: Generate a sorted list of random integers sortedrand.cpp -- Several algorithms for the task. Column 13: Set representations for the problem in Column 12 sets.cpp -- Several data structures for sets. genbins.c (Column 9) implements the bin data structure in C. Column 14: Heaps priqueue.cpp -- Implement and test priority queues. The column also uses sort.c (Column 11) for heapsort. Column 15: Strings wordlist.cpp -- List words in the file, using STL set. wordfreq.cpp -- List words in the file, with counts, using STL map. wordfreq.c -- Same as above, with hash table in C. longdup.c -- Find long repeated strings in input. markov.c -- Generate random text from input. markovhash.c -- Like markov.c, but with hashing. markovlet.c -- Letter-level markov text, simple algorithm. Appendix 3: Cost Models spacemod.cpp -- Space used by various records. timemod.c -- Table of times used by various C constructs. You may use this code for any purpose, as long as you leave the copyright notice and book citation attached.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值