[5794] Luo Valley [THUSC2015] decryption (analog)

Click here to see the problem surface

Generally meaning of the questions: '.' For a string, we add its one end, a character string regarded as a ring, from \ (n + 1 \) position to give OFF \ (n + 1 \) one The new string. This will now be \ (n-+. 1 \) (Minimum '' lexicographic order) new lexicographically sorted strings, given the sequentially ordered last bit in each string of characters, find the original string.

Foreword

See this problem in a coffee shop, graffiti on paper for a long time, and finally out of the mouth Hu.

But at the time it is a more complex, later thought to it sorted out when to eat, write out the last practice is very concise.

Hand play

First, let's look at a sample hand to play, perhaps what would have found.

The sample was given last bit string "AAAC.AB".

By the end of the string, in fact, the subject also indirectly gives us a message that string is clearly a ".AAAABC" consisting of seven characters.

And because it is based on lexicographic order, apparently the first pay-per-character should be ".AAAABC".

Put it into a matrix form, that is:

\[\left\{\begin{matrix}.&?&?&?&?&?&A\\A&?&?&?&?&?&A\\A&?&?&?&?&?&A\\A&?&?&?&?&?&C\\A&?&?&?&?&?&.\\B&?&?&?&?&?&A\\C&?&?&?&?&?&B\end{matrix}\right\}\]

Because the string to form a ring, i.e., a string in each of the above, in the first character in the original string of characters related to the last one in the back.

That is, '' followed by 'A', 'A' followed by '.' Or 'A' or 'A' or 'B', 'B' followed by a 'C', 'C' followed by 'A '.

Again because these strings are arranged in order according to the dictionary, so we can put behind each character can be followed by a sequence of characters that row, and then in turn fill in the square.

So we got:

\[\left\{\begin{matrix}.&A&?&?&?&?&A\\A&.&?&?&?&?&A\\A&A&?&?&?&?&A\\A&A&?&?&?&?&C\\A&B&?&?&?&?&.\\B&C&?&?&?&?&A\\C&A&?&?&?&?&B\end{matrix}\right\}\]

The above approach seems to point to inspire us, so we can think of routines:

"A." followed afterwards by 'A', "AA" followed afterwards '.' Or 'A', "CA" followed afterwards by 'A', ". A" followed afterwards by 'B', "AB" followed afterwards by 'C', "BC" followed afterwards by 'a'.

Fill in the grid, we get:

\[\left\{\begin{matrix}.&A&B&?&?&?&A\\A&.&A&?&?&?&A\\A&A&.&?&?&?&A\\A&A&A&?&?&?&C\\A&B&C&?&?&?&.\\B&C&A&?&?&?&A\\C&A&A&?&?&?&B\end{matrix}\right\}\]

According to this routine, we can easily fill this matrix intact, leaving the reader to try their own.

And if this program algorithm is \ (O (n ^ 2) \) complexity, we have been able to obtain \ (60 \) score good grades.

optimization

Next, how can we optimize it?

If you seriously to hand the sample comes to an end, you may be able to find a pattern: For each string, it always starts with a string transfer over.

The first string of the above example, it always gets the next character from the string above the fifth.

This makes us think that if we can directly determines the character of each string transferred from which strings, this question seems to be done.

And this is actually a very good demand.

Specifically, we first put the last one sort title character given, get first string.

Then we put every last bit character string as the first key, the first character is the second key, the third key number again ordered after the first sort of \ (i \) string of numbers that corresponding original string is to transfer its string.

If the transfer seen as a side, as long as we start from the first string, again traversing along the reverse side, each taking the original string (ie the title by lexicographical sort of string) in the first character (not difficult to find originally the first character of the string is the result of the first character to sort your keywords), is the answer.

As for why to do so, a bit difficult to describe, it is not clear on their own to draw map it should be quite clear that a good understanding.

Code

#include<bits/stdc++.h>
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
#define Reg register
#define RI Reg int
#define Con const
#define CI Con int&
#define I inline
#define W while
#define N 200000
using namespace std;
int n,m,a[N+5];struct data
{
    int H,T,p;I data(CI x=0,CI y=0,CI t=0):H(x),T(y),p(t){}
    I bool operator < (Con data& o) Con {return T^o.T?T<o.T:(H^o.H?H<o.H:p<o.p);}
}s[N+5];
class FastIO
{
    private:
        #define FS 100000
        #define tc() (A==B&&(B=(A=FI)+fread(FI,1,FS,stdin),A==B)?EOF:*A++)
        #define pc(c) (C==E&&(clear(),0),*C++=c)
        #define tn (x<<3)+(x<<1)
        #define D isdigit(c=tc())
        int T;char c,*A,*B,*C,*E,FI[FS],FO[FS],S[FS];
    public:
        I FastIO() {A=B=FI,C=FO,E=FO+FS;}
        Tp I void read(Ty& x) {x=0;W(!D);W(x=tn+(c&15),D);}
        Tp I void write(Ty x) {W(S[++T]=x%10+48,x/=10);W(T) pc(S[T--]);}
        Tp I void write(Con Ty& x,Con char& y) {write(x),pc(y);}
        I void clear() {fwrite(FO,1,C-FO,stdout),C=FO;}
}F;
int main()
{
    RI i;for(F.read(n),F.read(m),i=1;i<=n+1;++i) F.read(a[i]),s[i].T=a[i],s[i].p=i;//读入
    for(sort(a+1,a+n+2),i=1;i<=n+1;++i) s[i].H=a[i];sort(s+1,s+n+2);//排序求出首位字符,然后再次排序
    i=s[1].p;W(i^1) F.write(a[i],' '),i=s[i].p;return F.clear(),0;//遍历,输出首位字符,注意第一个'.'无需输出
}

Guess you like

Origin www.cnblogs.com/chenxiaoran666/p/Luogu5794.html