TIOJ 1446 - H遊戲密笈

Link: http://tioj.ck.tp.edu.tw/problems/1446

他要求在紙上輸出一行,但並不需要依序輸出,所以先用字典續排序每一行字串,使得連續兩次輸出有較大的共同前綴(可以不需要刪除)

AC code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string s[100010];
int prefix(const string &a,const string &b)
{
int len=min(a.size(),b.size());
for(int i=0;i<len;i++)
if(a[i]!=b[i])
return i;
return len;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);

int n;
cin>>n;
for(int i=0;i<n;i++)
cin>>s[i];
sort(s,s+n);
int ans=0;
string pre="";
for(int i=0;i<n;i++)
{
ans+=pre.size()+s[i].size()-prefix(pre,s[i])*2+1;
pre=s[i];
}
ans+=pre.size();
cout<<ans<<'\n';
}