2 条题解
-
0
#include <bits/stdc++.h> using namespace std; const int N = 605; char s[51]; int ch[N][26], id, ed[N], pre[N], v[N][1 << 12 | 1], c[N]; struct node { int c, pre; } a[N * (1 << 12 | 1)]; void ins(char *s, int x) { int p = 0; for (int i = 0; s[i]; i++) { int j = s[i] - 'A'; if (ch[p][j] == 0) ch[p][j] = ++id; p = ch[p][j]; } ed[p] |= 1 << (x - 1); } void build() { queue<int> Q; for (int i = 0; i < 26; i++) if (ch[0][i]) Q.push(ch[0][i]); while (!Q.empty()) { int x = Q.front(); Q.pop(); ed[x] |= ed[pre[x]]; for (int i = 0; i < 26; i++) { int &y = ch[x][i]; if (y == 0) y = ch[pre[x]][i]; else pre[y] = ch[pre[x]][i], Q.push(y); } } } int main() { int n, m; scanf("%d", &n); id = 0; memset(ch, 0, sizeof(ch)); memset(ed, 0, sizeof(ed)); for (int i = 1; i <= n; i++) { scanf("%s", s); ins(s, i); } memset(pre, 0, sizeof(pre)); build(); queue<pair<int, int>> Q; memset(v, 0, sizeof(v)); Q.push({0, 0}); v[0][0] = 1; int head = 0, tail = 0, pp; while (Q.size()) { int x = Q.front().first, st = Q.front().second; Q.pop(); if (st == ((1 << n) - 1)) { pp = head; break; } for (int i = 0; i < 26; i++) { int y = ch[x][i]; if (!v[y][st | ed[y]]) { v[y][st | ed[y]] = 1; Q.push({y, st | ed[y]}); tail++; a[tail] = node{i, head}; } } head++; } int cn = 0; while (pp) c[++cn] = a[pp].c, pp = a[pp].pre; for (int i = cn; i >= 1; i--) printf("%c", c[i] + 'A'); return 0; } -
-1
看完正解请自行前往此处吃史。
挑战评测机的卡常代码:
#include<bits/stdc++.h> using namespace std; const int N=610,M=5010; int ch[N][26],pre[N],ed[N],len,x; void ins(string s) { int n=s.size(),p=0; for(int i=0;i<n;i++) { int j=s[i]-'A'; if(!ch[p][j])ch[p][j]=++len; p=ch[p][j]; } if(ed[p])x--; else ed[p]=x; } void build() { deque<int>q; for(int i=0;i<26;i++)if(ch[0][i])q.push_back(ch[0][i]); while(!q.empty()) { int x=q.front();q.pop_front(); for(int j=0;j<26;j++) { int y=ch[x][j]; if(!y)ch[x][j]=ch[pre[x]][j]; else pre[y]=ch[pre[x]][j],q.push_back(y); } } } int dp[N][M],v[N]; int dfs(int x) { if(v[x]!=-1)return v[x]; v[x]=dfs(pre[x]); if(ed[x])v[x]|=(1<<(ed[x]-1)); return v[x]; } struct node{int f,id;string s;}; signed main() { int n;cin>>n; for(int i=1;i<=n;i++) { string s;cin>>s; x++;ins(s); } build(); memset(v,-1,sizeof(v));v[0]=0;for(int i=1;i<=len;i++)dfs(i); deque<node>q;q.push_back({0,0,""});dp[0][0]=1; while(!q.empty()) { node no=q.front();q.pop_front(); if(no.f==(1<<x)-1) { cout<<no.s; return 0; } for(int i=0;i<26;i++) { int j=ch[no.id][i],f=no.f|v[j]; if(!dp[j][f])dp[j][f]=1,q.push_back({f,j,no.s+(char)('A'+i)}); } } return 0; }
- 1
信息
- ID
- 2848
- 时间
- 2000ms
- 内存
- 128MiB
- 难度
- 8
- 标签
- 递交数
- 118
- 已通过
- 19
- 上传者