编程珠玑(第二版)2.1节第三个问题之变位词的实现

Doubi Gao3 posted @ 2013年12月30日 21:48 in essay with tags algorithim , 1383 阅读

今天翻了翻编程珠玑这本书,整体而言还是比较有趣的。可以说给了读者另一种认识编程的角度吧。

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
using namespace std;

const int maxn = 1100;
vector<string> A[maxn];

map<string, int> hash;

int main()
{
	hash.clear();
	
	string str[110];
	int n = 0, k = 0;
	
	while(cin>>str[n] && str[n] != "END")
	{
		string t = str[n];
		sort(t.begin(), t.end());
		if(!hash[t]) hash[t] = ++k;
		n++;
	}
	
	for(int i = 0; i < n; i++)
	{
		string t = str[i];
		sort(t.begin(), t.end());
		A[hash[t]].push_back(str[i]);
	}
	
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < A[i].size(); j++)
		{
			cout<<A[i][j]<<" ";
		}
		cout<<endl;
	}
	
	system("pause");
	return 0;
}


/*
input:
	
ptos
pots
stop
tops
XXXX
END

output:
ptos pots stop tops
XXXX
*/
MBOSE Model Paper Cl 说:
2022年8月25日 18:06

Meghalaya Board Model Paper 2023 Class 6 Pdf Download with Answers for Bengali Medium, English Medium, Hindi Medium, Urdu Medium & Students for Small Answers, Long Answer, Very Long Answer Questions, and Essay Type MBOSE Model Paper Class 6 Questions to Term1 & Term2 Exams at official website. New Exam Scheme or Question Pattern for Sammittive Assignment Exams (SA1 & SA2): Very Long Answer (VLA), Long Answer (LA), Small Answer (SA), Very Small Answer (VSA), Single Answer, Multiple Choice and etc.

seo service UK 说:
2024年2月21日 20:55

This is a great article thanks for sharing this informative information. I will visit your blog regularly for some latest post. I will visit your blog regularly for Some latest post


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter