当前位置:网站首页>洛谷P3065 [USACO12DEC]First! G 题解
洛谷P3065 [USACO12DEC]First! G 题解
2022-07-03 14:15:00 【q779】
洛谷P3065 [USACO12DEC]First! G 题解
题目链接:P3065 [USACO12DEC]First! G
题意:
Bessie一直在研究字符串。她发现,通过改变字母表的顺序,她可以按改变后的字母表来排列字符串(字典序大小排列)。
例如,Bessie发现,对于字符串串“omm”,“moo”,“mom”和“ommnom”,她可以使用标准字母表使“mom”排在第一个(即字典序最小),她也可以使用字母表“abcdefghijklonmpqrstuvwxyz”使得“omm”排在第一个。然而,Bessie想不出任何方法(改变字母表顺序)使得“moo”或“ommnom”排在第一个。
接下来让我们通过重新排列字母表的顺序来计算输入中有哪些字符串可以排在第一个(即字典序最小),从而帮助Bessie。
要计算字符串X和字符串Y按照重新排列过的字母表顺序来排列的顺序,先找到它们第一个不同的字母X[i]与Y[i],按重排后的字母表顺序比较,若X[i]比Y[i]先,则X的字典序比Y小,即X排在Y前;若没有不同的字母,则比较X与Y长度,若X比Y短,则X的字典序比Y小,即X排在Y前。
∑ ∣ S i ∣ ≤ 3 × 1 0 5 \sum \left|S_i\right| \le 3 \times 10^5 ∑∣Si∣≤3×105
显然trie树存字符串
对于每个字符串,我们都假设它可以成为最小字典序的字符
然后依次遍历它的每个字符,尽可能让它们成为最小的
具体的,对于字符 u u u ,如果我们想让它比 v v v 字典序小,
我们可以把 u u u 向 v v v 连一条有向边
显然无解的情况会成环,这个可以用topo来判
值得注意的是,如果一个串的子串也在输入数据中存在,那么它一定无解
时间复杂度 O ( 26 × ∑ ∣ S i ∣ ) O(26 \times \sum |S_i|) O(26×∑∣Si∣)
代码:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iomanip>
#include <queue>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define N (int)(3e4+15)
#define M (int)(3e5+15)
int n,ans;
string s[N];
bool ok[N];
struct Trie
{
int tot,trie[M][26],ed[M],e[35][35],in[26];
queue<int> q;
void insert(string str)
{
int u=0;
for(int i=0; i<str.size(); i++)
{
int c=str[i]-'a';
if(!trie[u][c])trie[u][c]=++tot;
u=trie[u][c];
}
ed[u]=1;
}
void topo()
{
while(!q.empty())q.pop();
for(int i=0; i<26; i++)
if(!in[i])q.push(i);
while(!q.empty())
{
int u=q.front();q.pop();
for(int c=0; c<26; c++)
if(e[u][c]&&(!(--in[c])))
q.push(c);
}
}
bool solve(string str)
{
int u=0;
memset(e,0,sizeof(e));
memset(in,0,sizeof(in));
for(int i=0; i<str.size(); i++)
{
if(ed[u])return 0;
int c=str[i]-'a';
for(int j=0; j<26; j++)
{
if(j!=c&&trie[u][j]&&!e[c][j])
e[c][j]=1,++in[j];
}
u=trie[u][c];
}
topo();
for(int i=0; i<26; i++)
if(in[i])return 0;
return 1;
}
}tr;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("check.in","r",stdin);
// freopen("check.out","w",stdout);
cin >> n;
for(int i=1; i<=n; i++)
{
cin >> s[i];
tr.insert(s[i]);
}
for(int i=1; i<=n; i++)
if(tr.solve(s[i]))
{
++ans;
ok[i]=1;
}
cout << ans << '\n';
for(int i=1; i<=n; i++)
if(ok[i])cout << s[i] << '\n';
return 0;
}
转载请说明出处
边栏推荐
- 战略、战术(和 OKR)
- Webpage connection database ~ simple implementation of addition, deletion, modification and query complete code
- 中感微冲刺科创板:年营收2.4亿净亏1782万 拟募资6亿
- 7-17 crawling worms (break exercise)
- Onmenusharetimeline custom shared content is invalid, and the title and icon are not displayed
- Although not necessarily the best, it must be the hardest!
- Invalid Z-index problem
- Exercise 8-2 calculate the sum and difference of two numbers
- Thread.sleep和TimeUnit.SECONDS.sleep的区别
- 洛谷P5194 [USACO05DEC]Scales S 题解
猜你喜欢
随机推荐
适用于XP的DDK
fpga阻塞赋值和非阻塞赋值
Scroll detection of the navigation bar enables the navigation bar to slide and fix with no content
7-16 find the set of integers that meet the given conditions
JS first summary
npm install卡住与node-npy的各种奇怪报错
[Jilin University] information sharing of postgraduate entrance examination and re examination
Polestar美股上市:5.5万台交付如何支持得起超200亿美元估值
Exercise 10-3 recursive implementation of exponential functions
超简单手机地图开发
7-18 finding the single root of polynomial by dichotomy
Exercise 9-3 plane vector addition
Canvas utility library fabric JS user manual
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
TS code automatically generates JS
Exercise 10-1 calculate the sum of 1 to n using recursive functions
中国PETG市场预测及战略研究报告(2022版)
洛谷P5194 [USACO05DEC]Scales S 题解
7-23 currency conversion (using array conversion)
Duet date picker (time plug-in that can manually enter the date)






![[clean up the extraordinary image of Disk C]](/img/0d/331115bc5d82d6337ae975a08494b2.jpg)


