1. 程式人生 > >[luogu P3065] [USACO12DEC]第一!First!

[luogu P3065] [USACO12DEC]第一!First!

could 很多 字符串 nes 直接 ret lan char san

[luogu P3065] [USACO12DEC]第一!First!

題目描述

Bessie has been playing with strings again. She found that by changing the order of the alphabet she could make some strings come before all the others lexicographically (dictionary ordering).

For instance Bessie found that for the strings "omm", "moo", "mom", and "ommnom" she could make "mom" appear first using the standard alphabet and that she could make "omm" appear first using the alphabet

"abcdefghijklonmpqrstuvwxyz". However, Bessie couldn‘t figure out any way to make "moo" or "ommnom" appear first.

Help Bessie by computing which strings in the input could be lexicographically first by rearranging the order of the alphabet. To compute if string X is lexicographically before string Y find the index of the first character in which they differ, j. If no such index exists then X is lexicographically before Y if X is shorter than Y. Otherwise X is lexicographically before Y if X[j] occurs earlier in the alphabet than Y[j].

給出n個字符串,問哪些串能在特定的字母順序中字典序最小。

輸入輸出格式

輸入格式:

  • Line 1: A single line containing N (1 <= N <= 30,000), the number of strings Bessie is playing with.

  • Lines 2..1+N: Each line contains a non-empty string. The total number of characters in all strings will be no more than 300,000. All characters in input will be lowercase characters ‘a‘ through ‘z‘. Input will contain no duplicate strings.

輸出格式:

  • Line 1: A single line containing K, the number of strings that could be lexicographically first.

  • Lines 2..1+K: The (1+i)th line should contain the ith string that could be lexicographically first. Strings should be output in the same order they were given in the input.

輸入輸出樣例

輸入樣例#1: 復制
4 
omm 
moo 
mom 
ommnom 
輸出樣例#1: 復制
2 
omm 
mom 

說明

The example from the problem statement.

Only "omm" and "mom" can be ordered first.

來點不是很難又不是很水的題目。

這一題最開始的想法就是建一棵字典樹trie,然後,對於每一個單詞,沿著字典樹中他的路徑走下去。

那怎麽判斷是否可行?如果按照貪心的想法,比如當前的節點優先級設為剩下(除去前幾個字母)最高的,這樣顯然會有反例。

那麽,我們想,安排字母的順序,優先級,我們想到了topo排序。

由於每一個節點下面,非當前路徑上的點的優先級小於路徑上的點,所以就可以建一條邊。

在這裏可以直接用鄰接矩陣,更方便,且效率也沒差到哪裏(因為可能有很多邊)。

然後,就進行topo排序了,如果可行就可以了。

還有需要註意的是,比如有兩個字符串:

wzz

wzzlihai

那麽,wzzlihai也不能以某種順序排到第一位。

那這個怎麽判呢?在每個單詞結束的時候都在結束點打個“結束”標記。

然後詢問時,如果路徑上某一個點(非最後一個)上有“結束”標記,則return 0。

code:

技術分享
  1 #pragma GCC optimize(2)
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 #include <iostream>
  6 #include <string>
  7 #include <queue>
  8 #define ms(a,x) memset(a,x,sizeof a)
  9 typedef long long LL;
 10 namespace fastIO {
 11     #define puc(c) putchar(c)
 12     inline int read() {
 13         int x=0,f=1; char ch=getchar();
 14         while (ch<0||ch>9) {
 15             if (ch==-) f=-f;
 16             ch=getchar();
 17         }
 18         while (ch>=0&&ch<=9) {
 19             x=(x<<3)+(x<<1)+ch-0;
 20             ch=getchar();
 21         }
 22         return x*f;
 23     }
 24     template <class T> inline void read(T &x=0) {
 25         T f=1; char ch=getchar();
 26         while (ch<0||ch>9) {
 27             if (ch==-) f=-f;
 28             ch=getchar();
 29         }
 30         while (ch>=0&&ch<=9) {
 31             x=(x<<3)+(x<<1)+ch-0;
 32             ch=getchar();
 33         }
 34         x*=f;
 35     }
 36     int cnt,w[20];
 37     template <class T> inline void write(T x) {
 38         if (x==0) {
 39             puc(0);
 40             return;
 41         }
 42         if (x<0) {
 43             x=-x;
 44             puc(-);
 45         }
 46         for (cnt=0; x; x/=10) w[++cnt]=x%10;
 47         for (; cnt; --cnt) puc(w[cnt]+48);
 48     }
 49     inline void newline() {
 50         puc(\n);
 51     }
 52     inline void newblank() {
 53         puc( );
 54     }
 55 }
 56 namespace OJ{
 57     void Online_Judge() {
 58         #ifndef ONLINE_JUDGE
 59             freopen("in.txt","r",stdin);
 60             freopen("out.txt","w",stdout);
 61         #endif
 62     }
 63 }
 64 using std::string;
 65 using std::queue;
 66 const int N=30005,L=300005,A=26;
 67 int n,cnt,len[N]; bool vis[N]; string s[N]; char ss[L];
 68 int tot,f[A][A],dg[N];
 69 queue <int> q;
 70 #define TrieNode node
 71 class TrieNode {
 72     private:
 73         bool end; node *ch[A];
 74     public:
 75         node() {
 76             end=0,ms(ch,0);
 77         }
 78         inline bool topo() {
 79             while (!q.empty()) q.pop();
 80             for (int i=0; i<A; ++i) {
 81                 for (int j=0; j<A; ++j) {
 82                     if (f[i][j]) ++dg[j];
 83                 }
 84             }
 85             for (int i=0; i<A; ++i) {
 86                 if (dg[i]==0) q.push(i);
 87             }
 88             if (q.empty()) return 0;
 89             for (int x; !q.empty(); ) {
 90                 x=q.front(),q.pop();
 91                 for (int i=0; i<A; i++) {
 92                     if (f[x][i]) {
 93                         --dg[i];
 94                         if (dg[i]==0) q.push(i);
 95                     }
 96                 }
 97             }
 98             for (int i=0; i<A; ++i) {
 99                 if (dg[i]!=0) return 0;
100             }
101             return 1;
102         }
103         inline void insert(node *u,char a[],int l) {
104             for (int i=0,x; i<l; ++i) {
105                 x=a[i]-a;
106                 if (u->ch[x]==0) {
107                     u->ch[x]=new node();
108                 }
109                 u=u->ch[x];
110             }
111             u->end=1;
112         }
113         inline bool reply(node *u,char a[],int l) {
114             ms(f,0),ms(dg,0);
115             for (int i=0,x; i<l; ++i) {
116                 x=a[i]-a;
117                 if (i<l-1&&u->ch[x]->end) return 0;
118                 for (int j=0; j<26; ++j) {
119                     if (u->ch[j]!=0&&j!=x) f[x][j]=1;
120                 }
121                 u=u->ch[x];
122             }
123             return topo();
124         }
125 }t,*rot;
126 int main() {
127     OJ::Online_Judge();
128     scanf("%d",&n),cnt=0,rot=new node();
129     for (int i=1; i<=n; ++i) {
130         scanf("%s",ss),len[i]=strlen(ss);
131         s[i]="";
132         for (int j=0; j<len[i]; ++j) {
133             s[i]=s[i]+ss[j];
134         }
135         t.insert(rot,ss,len[i]);
136     }
137     for (int i=1; i<=n; ++i) {
138         for (int j=0; j<len[i]; ++j) {
139             ss[j]=s[i][j];
140         }
141         cnt+=vis[i]=t.reply(rot,ss,len[i]);
142     }
143     printf("%d\n",cnt);
144     for (int i=1; i<=n; ++i) {
145         if (vis[i]) {
146             for (int j=0; j<len[i]; ++j) {
147                 putchar(s[i][j]);
148             }
149             putchar(\n);
150         }
151     }
152     return 0;
153 }
View Code

[luogu P3065] [USACO12DEC]第一!First!