首页 > 代码库 > UVa 11488 Hyper Prefix Sets

UVa 11488 Hyper Prefix Sets

方法:Trie

本题其实就是trie的实现,每个节点需要记录两个值,深度 和 visit的次数,答案便是 max(深度 * visit的次数)。

数组实现code:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
#include <fstream>
#include <cassert>
#include <unordered_map>
#include <cmath>
#include <sstream>
#include <time.h>
#include <complex>
#include <iomanip>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define FOR(a,b,c) for (ll (a)=(b);(a)<(c);++(a))
#define FORN(a,b,c) for (ll (a)=(b);(a)<=(c);++(a))
#define DFOR(a,b,c) for (ll (a)=(b);(a)>=(c);--(a))
#define FORSQ(a,b,c) for (ll (a)=(b);(a)*(a)<=(c);++(a))
#define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
#define FOREACH(a,b) for (auto &(a) : (b))
#define rep(i,n) FOR(i,0,n)
#define repn(i,n) FORN(i,1,n)
#define drep(i,n) DFOR(i,n-1,0)
#define drepn(i,n) DFOR(i,n,1)
#define MAX(a,b) a = Max(a,b)
#define MIN(a,b) a = Min(a,b)
#define SQR(x) ((LL)(x) * (x))
#define Reset(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(v) v.begin(),v.end()
#define ALLA(arr,sz) arr,arr+sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(all(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr,sz) sort(ALLA(arr,sz))
#define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
#define PERMUTE next_permutation
#define TC(t) while(t--)
#define forever for(;;)
#define PINF 1000000000000
#define newline ‘\n‘

#define test if(1)if(0)cerr
using namespace std;
  using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
typedef pair<double,double> dd;
typedef pair<char,char> cc;
typedef vector<ii> vii;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> l4;
const double pi = acos(-1.0);

const int maxnode = 1e7;
const int sigma_size = 2;
struct Trie
{
    int ch[maxnode][sigma_size];
    int val[maxnode];
    int depth[maxnode];
    int sz;
    int ans;
    Trie()
    {
        sz = 1;
        ans = 0;
        Reset(ch[0], 0);
    }
    void reset()
    {
        sz = 1;
        ans = 0;
        Reset(ch[0], 0);
    }
    void insert(string &str, int v)
    {
        int u = 0, n = str.length();
        for (int i = 0; i < n; ++i)
        {
            int c = str[i] == ‘1‘;
            if (!ch[u][c])
            {
                Reset(ch[sz], 0);
                val[sz] = 0;
                ch[u][c] = sz++;
            }
            u = ch[u][c];
            depth[u] = i+1;
            val[u] += 1;
            ans = max(ans, depth[u]*val[u]);
        }
    }
};
Trie root;
int main()
{
    int T;
    cin >> T;
    cerr << T << newline;
    repn(kase, T)
    {
        int n;  cin >> n;
        root.reset();
        rep(i, n)
        {
            string str; cin >> str;
            root.insert(str, 0);
        }
        cout << root.ans << newline;
    }
}

 

心血来潮用动态分配空间写了一个,注意要合理释放空间,比如写一个delete(node * root) 的函数,不过注意最好不要写成recursive的,容易爆栈;或者维护两个stack or queue,储存使用的node* 和 回收不用的node*,code 如下

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
#include <fstream>
#include <cassert>
#include <unordered_map>
#include <cmath>
#include <sstream>
#include <time.h>
#include <complex>
#include <iomanip>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define FOR(a,b,c) for (ll (a)=(b);(a)<(c);++(a))
#define FORN(a,b,c) for (ll (a)=(b);(a)<=(c);++(a))
#define DFOR(a,b,c) for (ll (a)=(b);(a)>=(c);--(a))
#define FORSQ(a,b,c) for (ll (a)=(b);(a)*(a)<=(c);++(a))
#define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
#define FOREACH(a,b) for (auto &(a) : (b))
#define rep(i,n) FOR(i,0,n)
#define repn(i,n) FORN(i,1,n)
#define drep(i,n) DFOR(i,n-1,0)
#define drepn(i,n) DFOR(i,n,1)
#define MAX(a,b) a = Max(a,b)
#define MIN(a,b) a = Min(a,b)
#define SQR(x) ((LL)(x) * (x))
#define Reset(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(v) v.begin(),v.end()
#define ALLA(arr,sz) arr,arr+sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(all(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr,sz) sort(ALLA(arr,sz))
#define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
#define PERMUTE next_permutation
#define TC(t) while(t--)
#define forever for(;;)
#define PINF 1000000000000
#define newline ‘\n‘

#define test if(1)if(0)cerr
using namespace std;
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
typedef pair<double,double> dd;
typedef pair<char,char> cc;
typedef vector<ii> vii;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> l4;
const double pi = acos(-1.0);

struct node
{
    node *child[2];
    int depth, cnt;
    node():depth(0), cnt(0)
    {
        child[0] = child[1] = nullptr;
    }
};
stack<node*> ready;
stack<node*> inuse;
node* root;
node* add(int depth)
{
    if (ready.empty())
    {
        node* temp = new node();
        ready.push(temp);
    }
    auto ret = ready.top();   ready.pop();
    ret-> depth = depth;
    inuse.push(ret);
    return ret;
    
}
int ans;
void add(const string &str)
{
    auto cur = root;
    int len = str.length();
    for (int i = 0; i < len; ++i)
    {
        bool c = str[i]==‘1‘;
        if (cur->child[c] == nullptr)
        {
            cur->child[c] = add(i+1);
        }
        cur = cur->child[c];
        cur->cnt += 1;
        
        ans = max(ans, cur->cnt * cur->depth);
    }
}
void clear()
{
    root->child[0] = root->child[1] = nullptr;
    while (!inuse.empty())
    {
        auto temp = inuse.top();
        temp->cnt = 0;
        temp->child[0] = temp->child[1] = nullptr;
        ready.push(temp);
        inuse.pop();
    }
}
int main()
{
    root = new node();
    int T;
    cin >> T;
    cerr << T << newline;
    repn(kase, T)
    {
        int n;  cin >> n;
        ans = 0;
        rep(i, n)
        {
            string str; cin >> str; add(str);
        }
        cout << ans << newline;
        clear();
    }
}

  

UVa 11488 Hyper Prefix Sets