UVa 1519 Dictionary Size

方法:Trie

看了题解,有两种做法,大致是相通的。这道题重点在于如何判重。

建立两个trie,取名prefix 和 suffix。把所有string插入第一个trie,每个节点就代表一种prefix。同理,把所有string反转之后插入第二个trie,每个节点就代表一个suffix。如果没有重复的话,那么结果就是prefix.size * suffix.size。如何判重呢?如果一个长度至少为2的前缀p以字符c结尾,并且有一个长度至少为2的后缀 s以c开始,那么 p+s.substring(1) == p.substring(0, p.length()-1) + s。 所以,将trie中每个节点所代表的字符的数量记录下来,记在cnt[26], 所用重复的个数即 sum(prefix.cnt[i] * suffix.cnt[i]) for i = 0,1,2,..,25。

注意,我们有漏掉一种dictionary word 长度为1的情况,所以要把这些加起来。

code:

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <algorithm>
  4 #include <iostream>
  5 #include <string>
  6 #include <vector>
  7 #include <stack>
  8 #include <bitset>
  9 #include <cstdlib>
 10 #include <cmath>
 11 #include <set>
 12 #include <list>
 13 #include <deque>
 14 #include <map>
 15 #include <queue>
 16 #include <fstream>
 17 #include <cassert>
 18 #include <unordered_map>
 19 #include <unordered_set>
 20 #include <cmath>
 21 #include <sstream>
 22 #include <time.h>
 23 #include <complex>
 24 #include <iomanip>
 25 #define Max(a,b) ((a)>(b)?(a):(b))
 26 #define Min(a,b) ((a)<(b)?(a):(b))
 27 #define FOR(a,b,c) for (ll (a)=(b);(a)<(c);++(a))
 28 #define FORN(a,b,c) for (ll (a)=(b);(a)<=(c);++(a))
 29 #define DFOR(a,b,c) for (ll (a)=(b);(a)>=(c);--(a))
 30 #define FORSQ(a,b,c) for (ll (a)=(b);(a)*(a)<=(c);++(a))
 31 #define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
 32 #define FOREACH(a,b) for (auto &(a) : (b))
 33 #define rep(i,n) FOR(i,0,n)
 34 #define repn(i,n) FORN(i,1,n)
 35 #define drep(i,n) DFOR(i,n-1,0)
 36 #define drepn(i,n) DFOR(i,n,1)
 37 #define MAX(a,b) a = Max(a,b)
 38 #define MIN(a,b) a = Min(a,b)
 39 #define SQR(x) ((LL)(x) * (x))
 40 #define Reset(a,b) memset(a,b,sizeof(a))
 41 #define fi first
 42 #define se second
 43 #define mp make_pair
 44 #define pb push_back
 45 #define all(v) v.begin(),v.end()
 46 #define ALLA(arr,sz) arr,arr+sz
 47 #define SIZE(v) (int)v.size()
 48 #define SORT(v) sort(all(v))
 49 #define REVERSE(v) reverse(ALL(v))
 50 #define SORTA(arr,sz) sort(ALLA(arr,sz))
 51 #define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
 52 #define PERMUTE next_permutation
 53 #define TC(t) while(t--)
 54 #define forever for(;;)
 55 #define PINF 1000000000000
 56 #define newline ‘\n‘
 57
 58 #define test if(1)if(0)cerr
 59 using namespace std;
 60 using namespace std;
 61 typedef vector<int> vi;
 62 typedef vector<vi> vvi;
 63 typedef pair<int,int> ii;
 64 typedef pair<double,double> dd;
 65 typedef pair<char,char> cc;
 66 typedef vector<ii> vii;
 67 typedef long long ll;
 68 typedef unsigned long long ull;
 69 typedef pair<ll, ll> l4;
 70 const double pi = acos(-1.0);
 71
 72 const int maxnode = 4e5+5, sigma_size = 26;
 73 struct Trie
 74 {
 75     int g[maxnode][sigma_size], sz;
 76     int cnt[sigma_size];
 77     void init()
 78     {
 79         sz = 1; Reset(g[0], 0);
 80         Reset(cnt, 0);
 81     }
 82     int idx(char c)
 83     {
 84         return c - ‘a‘;
 85     }
 86     void insert(const string &str)
 87     {
 88         int u = 0, n = str.length();
 89         rep(i, n)
 90         {
 91             int c = idx(str[i]);
 92
 93             if (!g[u][c])
 94             {
 95                 Reset(g[sz], 0);
 96                 g[u][c] = sz++;
 97                 if (i)  cnt[c] += 1;
 98             }
 99             u = g[u][c];
100         }
101     }
102 };
103 Trie prefix, suffix;
104 bitset<sigma_size> vis;
105 int n;
106
107 int main()
108 {
109
110     ios::sync_with_stdio(false);
111     cin.tie(0);
112     while (cin >> n)
113     {
114         string line;
115         prefix.init(), suffix.init();
116         vis.reset();
117         rep(i, n)
118         {
119             cin >> line;
120             if (line.length() == 1) vis[line[0]-‘a‘] = true;
121             prefix.insert(line);
122             reverse(line.begin(), line.end());
123             suffix.insert(line);
124         }
125         ll ans = (ll) (prefix.sz-1) * (suffix.sz-1);
126         rep(i, sigma_size)
127         {
128             ans -= (ll) prefix.cnt[i] * suffix.cnt[i] - vis[i];
129         }
130
131         cout << ans << newline;
132     }
133
134
135
136
137
138 }

时间: 2024-08-21 23:03:14

UVa 1519 Dictionary Size的相关文章

uva 1519 - Dictionary Size(字典树)

题目链接:uva 1519 - Dictionary Size 题目大意:给出n个字符串组成的字典,现在要添加新的单词,从已有单词中选出非空前缀和非空后缀,组成新单词.问说能组成多少个单词. 解题思路:建立一棵前缀树和一棵后缀树,有多少节点即为有多少个前缀,扣除中间的部分即可加上长度为1的字符串即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const

UVA 1519 - Dictionary Size(Trie树)

UVA 1519 - Dictionary Size 题目链接 题意:有一个字典,里面包含一些词,要求组合新词,新词必须来自原字典,或者由原字典的字符串的非空前缀和非空后缀组成,问一共能组成多少个新词 思路:建Trie树,可以求出不同的前缀和后缀个数,然后相乘,这样做会有一部分重复的 比如Aaaa,aaaA的情况,就重复了,去重的方法可以推理出来 假设前缀A后面有x个a,后缀y个a后面一个A,那么这x和y个一共一边各有(x+1)和(y+1)种情况,相乘一共是(x+1)(y+1)种,那么实际上一共

Dictionary Size

uvaLive5913:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3924 题意:给你n个串,然后你可以用串的非空前缀和非空后缀组成新的单词,问形成新的加上原来的一共有多少种不同的串. 题解:很容易想到,先计算出前缀有多少种,后缀有多少种,然后prenum*sufnum即可,但是这样会有重复计算,必须把重复的减去.所以要

BZOJ3806: Neerc2011 Dictionary Size

题解: 这题搞得我真是酸(dan)爽(teng) 原来一直不会,一定会用到什么神奇的东西.因为重复的不知道如何计算. 今天中午睡起来忽然想到好像可以在正trie上故意走无出边,因为这样就保证了这次统计的所有字符串在它的孩子都不会再次被统计到. 然后感觉这题就解了. 然后和别人的程序对拍发现居然一直少计算了什么!去多了? 然后发现没有算上trie上的节点,前面只考虑了在trie后面加字符串... 然后发现好像正反都得特判,然后发现又有重复的!!!这怎么搞!!! 感觉AC无望便去颓了两集火影...

Python dictionary implementation

Python dictionary implementation http://www.laurentluce.com/posts/python-dictionary-implementation/ August 29, 2011 This post describes how dictionaries are implemented in the Python language. Dictionaries are indexed by keys and they can be seen as

JavaScript Dictionary

function Dictionary() { var items = {}; this.has = function(key) { return key in items } this.set = function(key, value) { items[key] = value } this.remove = function(key) { if (this.has(key)) { delete items[key]; return true } return false } this.ge

你真的了解字典(Dictionary)吗?

从一道亲身经历的面试题说起 半年前,我参加我现在所在公司的面试,面试官给了一道题,说有一个Y形的链表,知道起始节点,找出交叉节点. 为了便于描述,我把上面的那条线路称为线路1,下面的称为线路2. 思路1 先判断线路1的第一个节点的下级节点是否是线路2的第一个节点,如果不是,再判断是不是线路2的第二个,如果也不是,判断是不是第三个节点,一直到最后一个. 如果第一轮没找到,再按以上思路处理线路一的第二个节点,第三个,第四个... 找到为止. 时间复杂度n2,相信如果我用的是这种方法,可肯定被Pass

javascript字典数据结构常用功能实现

必知必会啊. function Dictionary(){ var items = {}; this.has = function (key) { return key in items; }; this.set = function(key, value){ items[key] = value; }; this.remove = function(key){ if (this.has(key)){ delete items[key]; return true; } return false;

Python自动化开发从浅入深-语言基础(collection)

-- collection是对内置数据类型的一种扩充,其主要扩充类型包括: 1.namedtuple(): 生成可以使用名字来访问元素内容的tuple子类,以增强可读性. def namedtuple(typename, field_names, verbose=False, rename=False): """Returns a new subclass of tuple with named fields. 返回一个新的命名域元组子类,typename为类型名,field