Codeforces Round #401 (Div. 2) D. Cloud of Hashtags

题目链接:D. Cloud of Hashtags

题意:

给你n个字符串,让你删后缀,使得这些字符串按字典序排列,要求是删除的后缀最少

题解:

由于n比较大,我们可以将全部的字符串存在一个数组里面,然后记录一下每个字符串的开始位置和长度,然后从下面往上对比。

如果str[i][j]<str[i+1][j],直接退出,因为这里已经成为字典序。

如果str[i][j]>str[i+1][j],那么就把str[i][j]=0,表示从这里开始的后缀全部删掉。

str[i][j]==str[i+1][j]就继续寻找。

注意:数组要开N*3,因为包括结束符。

 1 #include<bits/stdc++.h>
 2 #define F(i,a,b) for(int i=a;i<=b;++i)
 3 using namespace std;
 4 typedef long long ll;
 5 
 6 const int N=5e5+7;
 7 char str[N*3];
 8 int st[N],len[N],n,ed;
 9 
10 char find(int i,int j){return str[st[i]+j];}
11 
12 inline void output(int s)
13 {
14     int now=s;
15     while(1)
16     {
17         if(!str[now])return;
18         putchar(str[now++]);
19     }
20 }
21 
22 int main(){
23     scanf("%d",&n);
24     F(i,1,n)
25     {
26         st[i]=ed;
27         scanf("%s",str+ed);
28         len[i]=strlen(str+ed);
29         ed+=len[i]+1;
30     }
31     for(int i=n-1;i>0;i--)
32     {
33         F(j,1,len[i]-1)
34         {
35             int tmp=find(i+1,j)-find(i,j);
36             if(tmp<0)
37             {
38                 str[st[i]+j]=0;
39                 break;
40             }if(tmp>0)break;
41         }
42     }
43     F(i,1,n)output(st[i]),puts("");
44     return 0;
45 }
View Code
原文地址:https://www.cnblogs.com/bin-gege/p/6443768.html