Codeforces Round #401 (Div. 2) D. Cloud of Hashtags
生活随笔
收集整理的這篇文章主要介紹了
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
總結
以上是生活随笔為你收集整理的Codeforces Round #401 (Div. 2) D. Cloud of Hashtags的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 函数,游标与存储过程的综合应用
- 下一篇: MyBatis 事务源码分析