[原创]SPOJ KAOS [Trie]【字符串】
[原创]SPOJ KAOS [Trie]【字符串】
2017-01-05 22:17:18 Tabris_ 阅读数:399
博客爬取于2020-06-14 22:42:27
以下为正文
版权声明:本文为Tabris原创文章,未经博主允许不得私自转载。
https://blog.csdn.net/qq_33184171/article/details/54098779
题目连接:http://www.spoj.com/problems/KAOS/
基本都是借(chao)鉴(xi)WannaflyUnion
------------------------------------------------------.
KAOS - Kaos
kaos
Little Lovro likes to play games with words. During the last few weeks he realized that some words don’t like each other.
The words A and B don’t like each other if the word A is lexicographically before the word B, but the word B’ is lexicographically before the word A’, where X’ stands for the word X reversed (if X = “kamen”, then X’ = “nemak”). For example, the words “lova” and “novac” like each other, but the words “aron” and “sunce” don’t like each other.
Given some set of the words, we define the degree of chaos of the set as the number of pairs of different words that don’t like each other.
Write a program that, given a set of words, finds the chaos degree for the set.
input data
The first line of input contains an integer N, 2 ≤ N ≤ 100 000.
Each of the following N lines contains one word – a sequence of at most 10 lowercase letters of the English alphabet (‘a’ – ‘z’). There will be no two identical words in the set.
output data
The first and only line of output should contain a single integer – the chaos degree of the given set of words.
Note: use 64-bit signed integer type (int64 in Pascal, long long in C/C++)
examples
|||||
|-|-|-|
|input | input | input|
|2 | 4 | 14|
|lopta | lova | branimir|
|kugla | novac | vladimir|
| | aron | tom|
|output | sunce | kruz|
|0 | | bred|
| | output | pit|
|| 3 | zemlja|
|| | nije|
|| | ravna|
| | | ploca|
|| | ko|
| | | je|
| | | zapalio|
| | | zito|
| | | output|
| | | 48|
------------------------------------------------------.
题目大意:
就是给你一堆字符串,问你有多少对满足,正序时字典序a>b且逆序时字典序b>a的对数
解题思路:
很容易想到先对字符串按照字典序排序一遍,然后问题就能简化为仅仅判断逆序时字典序的对数就好了.
但是对于如何统计这个没有什么好的思路,最后看了WannaFlyUnion的题解才想到用Trie就好了…(不会字符串的渣渣根本没有向这方面想)
然后才惊人的发现,Trie树居然也可以用静态数组来实现,…
然后学习了… 这样就成了简单的Trie入门题了…
以下借(chao)鉴(xi)WannaflyUnion的题解代码
----------------------------------------------------------------------------------.
1 | # include <bits/stdc++.h> |