Luogu P4341 [BJWC2010] alien contact

Title Description

Violence ideas:

Enumeration value substring violence, inserted in the trie, lexicographical ordering of the output end is greater than 1

Time complexity: n3

The results: TLE 0 points

. . . Very awkward a little left


Correct 思路

After observed, can enumerate only the suffix, when the output value of the sum of statistics

code


#include<bits/stdc++.h>

using namespace std;

const int MAXN=1e7;

int n,cnt;

string ss;

struct node{

 int sum,end,son[2];

}trie[MAXN];

void insert(string s)

{

 int now=0,x;

 for(int i=0;i<s.length();i++){

  x=trie[now].son[s[i]-'0'];

  if(x==0){

   x=trie[now].son[s[i]-'0']=++cnt;

  }

  trie[x].sum++;

  now=x;

 }

}

 

void build()

{

 for(int i=0;i<n;i++){

   insert(ss.substr(i,n-i));

 }

}

 

void query(int now)

{

 if(trie[now].sum>1){

  cout<<trie[now].sum<<endl;

 }

 if(trie[now].son[0]){

  query(trie[now].son[0]);

 }

 if(trie[now].son[1]){

  query(trie[now].son[1]);

 }

}

 

int main()

{

// freopen(".in","r",stdin);

// freopen(".out","w",stdout);

 cin>>n>>ss;

 build();

 query(0);

 return 0;

}

It is heard positive solution SA

But I will not die too weak


upd 2019.9.13

markdown bombed

And re-issued again

Guess you like

Origin www.cnblogs.com/zhu-chen/p/11516176.html