strGame: Game Theory, trie

A very interesting question. On the Game Theory.

The best think about their own?

We first consider the case of only one game.

This problem is significantly away bit by bit in the string, the string established trie.

Eventual establishment of a leaf node trie is a losing position.

For non-leaf node, if it has one son is doomed to failure node, then the node is a node win. (Mex similar function)

So if the root win, then that is the upper hand to win, otherwise the flip win.

If you win the final round of escape, then two people will need to compete for the final round of escape, so they want to win the penultimate round.

The penultimate round and the final round is the same, then the penultimate round win is also the way of escape. The penultimate round of FLAC whole game is to win.

And so on up to the last third round, fourth round of the countdown. . . Until the first round, they are the same.

So, if a round FLAC win, then the whole game Dirty win.

The remaining case is to win the upper hand, it is to fight for the upper hand, then we would try to lose the penultimate round.

How to determine whether the upper hand must let yourself lose a game?

As long as the leaf nodes of the trie node instead like a win, pitted again.

So if you can just get yourself bound to lose a game, but also must let yourself win a game.

So in addition to the final round he can allow himself to lose in order to get the upper hand, until the last round let yourself win.

So, if a round to win the upper hand, the upper hand in the game is also contrary to win (ie, can allow himself must lose), then Pure normal game win.

The rest is to win the upper hand in a game, but can not win (that is, they can not necessarily lose) in the opposite game.

The final round is to win the upper hand.

The penultimate round of the final round to compete for the upper hand, so to lose, so the penultimate round of FLAC win in the whole game.

The third last round to compete for the penultimate round of FLAC, to win, so the reciprocal of the third round to win the upper hand in the whole game.

And so on. . .

So, if an upper hand to win the game, but instead flip to win the game (ie the upper hand can not allow ourselves to lose), the total number of rounds is odd, the upper hand to win. Otherwise, FLAC win.

Good question.

Hearty think for themselves (self-study mathematics inspired ++)

 1 #include<cstdio>
 2 #include<cstring>
 3 using namespace std;
 4 int k,trie[100005][27],w[2][100005],cnt,n,rt,len;char s[100005];
 5 void insert(int &p,int al){
 6     if(!p)p=++cnt;if(al==len)return;
 7     insert(trie[p][s[al]-'a'],al+1);
 8 }
 9 void dfs(int p){
10     int hs=0;w[0][p]=w[1][p]=0;
11     for(int i=0;i<=25;++i)if(trie[p][i]){hs=1;break;}
12     if(!hs){w[0][p]=1;w[1][p]=0;return;}
13     for(int i=0;i<=25;++i)if(trie[p][i]){
14         dfs(trie[p][i]);
15         if(!w[0][trie[p][i]])w[0][p]=1;
16         if(!w[1][trie[p][i]])w[1][p]=1;
17     }
18 }
19 int main(){
20     int t;scanf("%d",&t);
21     while(t--){
22         scanf("%d%d",&n,&k);rt=cnt=0;memset(trie,0,sizeof trie);
23         for(int i=1;i<=n;++i)scanf("%s",s),len=strlen(s),insert(rt,0);
24         dfs(rt);//for(int i=1;i<=cnt;++i)printf("%d %d\n",w[0][i],w[1][i]);
25         if(!w[1][rt])puts("Dirty");
26         else if(w[0][rt])puts("Pure");
27         else if(k&1)puts("Pure");
28         else puts("Dirty");
29     }
30 }
View Code

 

Guess you like

Origin www.cnblogs.com/hzoi-DeepinC/p/11564050.html