Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 1507 Accepted Submission(s): 744
Problem Description
The contest is beginning! While preparing the contest, iSea wanted to print the teams' names separately on a single paper.
Unfortunately, what iSea could find was only an ancient printer: so ancient that you can't believe it, it only had three kinds of operations:
● 'a'-'z': twenty-six letters you can type
● 'Del': delete the last letter if it exists
● 'Print': print the word you have typed in the printer
The printer was empty in the beginning, iSea must use the three operations to print all the teams' name, not necessarily in the order in the input. Each time, he can type letters at the end of printer, or delete the last letter, or print the current word. After printing, the letters are stilling in the printer, you may delete some letters to print the next one, but you needn't delete the last word's letters.
iSea wanted to minimize the total number of operations, help him, please.
Input
There are several test cases in the input.
Each test case begin with one integer N (1 ≤ N ≤ 10000), indicating the number of team names.
Then N strings follow, each string only contains lowercases, not empty, and its length is no more than 50.
The input terminates by end of file marker.
Output
For each test case, output one integer, indicating minimum number of operations.
Sample Input
2
freeradiant
freeopen
Sample Output
21
Hint
The sample's operation is:
f-r-e-e-o-p-e-n-Print-Del-Del-Del-Del-r-a-d-i-a-n-t-Print
、
Trie樹:
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <set>
//#define ONLINE_JUDGE
#define eps 1e-8
#define INF 0x7fffffff //INT_MAX
#define inf 0x3f3f3f3f //int??????????????????
#define FOR(i,a) for((i)=0;i<(a);(i)++) //[i,a);
#define MEM(a) (memset((a),0,sizeof(a)))
#define sfs(a) scanf("%s",a)
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define pfs(a) printf("%s\n",a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,c)scanf("%d%d%d",&a,&b,&c)
#define for1(i,a,b) for(int i=(a);i<b;i++)
#define for2(i,a,b) for(int i=(a);i<=b;i++)
#define for3(i,a,b)for(int i=(b);i>=a;i--)
#define MEM1(a) memset(a,0,sizeof(a))
#define MEM2(a) memset(a,-1,sizeof(a))
#define MEM3(a) memset(a,0x3f,sizeof(a))
#define LL __int64
const double PI = acos(-1.0);
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> inline T Min(T a, T b) { return a < b ? a : b; }
template<class T> inline T Max(T a, T b) { return a > b ? a : b; }
using namespace std;
template<class T>
T Mint(T a, T b, T c) {
if (a>b) {
if (c>b)
return b;
return c;
}
if (c>a)
return a;
return c;
}
template<class T>
T Maxt(T a, T b, T c) {
if (a>b) {
if (c>a)
return c;
return a;
}
else if (c > b)
return c;
return b;
}
const int maxn=26;
int T,n,m,k;
char s[10005];
int cnt;
typedef struct Trie{
Trie *next[26];
int v;
}Trie;
Trie *root;
void CreatTrie(char *str){
int len=strlen(str);
Trie *p=root,*q;
for1(i,0,len){
int id=str[i]-'a';
if(p->next[id]==NULL){
q=(Trie*)malloc(sizeof(Trie));
q->v=1;
for1(j,0,maxn){
q->next[j]=NULL;
}
p->next[id]=q;
p=p->next[id];
cnt++;
}
else{
p->next[id]->v++;
p=p->next[id];
}
}
p->v=-1;
}
int FindTrie(char *str){
int len=strlen(str);
Trie *p=root;
for1(i,0,len){
int id=str[i]-'a';
p=p->next[id];
if(p==NULL)
return 0;
if(p->v==-1)
return p->v;
}
return -1;
}
void del(Trie *T){
if(T==NULL)
return ;
for1(i,0,maxn){
if(T->next[i]!=NULL)
del(T->next[i]);
}
free(T);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
#endif
while(~sf(n)){
root=(Trie*)malloc(sizeof(Trie));
for1(i,0,maxn){
root->next[i]=NULL;
}
cnt=0;
int max=0;
for1(i,0,n){
sfs(s);
CreatTrie(s);
int len=strlen(s);
if(len>max) max=len;
}
pf(2*cnt+n-max);
del(root);
}
return 0;
}
貪心:
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <set>
//#define ONLINE_JUDGE
#define eps 1e-8
#define INF 0x7fffffff //INT_MAX
#define inf 0x3f3f3f3f //int??????????????????
#define FOR(i,a) for((i)=0;i<(a);(i)++) //[i,a);
#define MEM(a) (memset((a),0,sizeof(a)))
#define sfs(a) scanf("%s",a)
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define pfs(a) printf("%s\n",a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,c)scanf("%d%d%d",&a,&b,&c)
#define for1(i,a,b) for(int i=(a);i<b;i++)
#define for2(i,a,b) for(int i=(a);i<=b;i++)
#define for3(i,a,b)for(int i=(b);i>=a;i--)
#define MEM1(a) memset(a,0,sizeof(a))
#define MEM2(a) memset(a,-1,sizeof(a))
#define MEM3(a) memset(a,0x3f,sizeof(a))
#define LL __int64
const double PI = acos(-1.0);
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> inline T Min(T a, T b) { return a < b ? a : b; }
template<class T> inline T Max(T a, T b) { return a > b ? a : b; }
using namespace std;
template<class T>
T Mint(T a, T b, T c) {
if (a>b) {
if (c>b)
return b;
return c;
}
if (c>a)
return a;
return c;
}
template<class T>
T Maxt(T a, T b, T c) {
if (a>b) {
if (c>a)
return c;
return a;
}
else if (c > b)
return c;
return b;
}
const int maxn=26;
int T,n,m,k;
string s[10005];
int fun(string a,string b){
int i,lena=a.length(),lenb=b.length();
for(i=0;i<lena&&i<lenb&&a[i]==b[i];i++);
return lena+lenb-2*i;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
#endif
while(~sf(n)){
for1(i,0,n){
cin>>s[i];
}
sort(s,s+n);
int ans=s[0].length()+1;
int max=s[0].length();
for1(i,1,n){
int len=s[i].length();
if(len>max)
max=len;
ans+=fun(s[i],s[i-1])+1;
}
pf(ans+s[n-1].length()-max);
}
return 0;
}