hihocoder-Week197-逆序单词

hihocoder-Week197-逆序单词

逆序单词

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

在英文中有很多逆序的单词,比如dog和god,evil和live等等。

现在给出一份包含N个单词的单词表,其中每个单词只出现一次,请你找出其中有多少对逆序单词。

输入

第1行:1个整数,N,表示单词数量。2≤N≤50,000。

第2..N+1行:每行1个单词,只包含小写字母,每个单词长度不超过16个字母。保证每个单词只出现一次,且不会出现回文单词(即一个单词倒序还是它自己,比如eye)。

输出

第1行:1个整数,表示单词表中逆序单词的对数。

样例输入
6
dog
live
hiho
evil
coder
god
样例输出
2

很简单的一道题目,使用unordered_set可以通过,想要高性能一点可使用 Tie树。

#include <iostream> 
#include <string> 
#include <algorithm> 
#include <unordered_set> 
using namespace std; 

int main()
{

	int n, ans = 0; 
	string s; 
	unordered_set<string> st; 
	cin >> n; 
	for(int i=0; i<n; ++i){
		cin >> s; 
		if( st.count(s) > 0 ){
			++ans; 
		} else {
			reverse(s.begin(), s.end()); 
			st.insert( s ); 
		} 
	}
	cout << ans << endl;  
	return 0; 
}

  

原文地址:https://www.cnblogs.com/zhang-yd/p/8762896.html