字典樹 Ancient Printer

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

 

 

Author

iSea @ WHU

 

 

Source

2010 ACM-ICPC Multi-University Training Contest(3)——Host by WHU

讀題很明顯是個tire樹 敲鍵盤的次數 = n(每個單詞要print一次) + 2 * cnt(每個單詞打了要刪去) - Maxstr(最後一個最長的不用刪去)

#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <stdio.h>
#include <ctype.h>
#include <bitset>
#include <sstream>
#define  LL long long
#define  ULL unsigned long long
#define mod 10007
#define INF 0x7ffffff
#define mem(a,b) memset(a,b,sizeof(a))
#define MODD(a,b) (((a%b)+b)%b)
using namespace std;
const int maxn = 5e5 + 5;
const int NUM = 1e6 + 5;
int n,cnt;
int temp[maxn][30];
void buildTree(char *str)
{
    int tot = 0;
    int len = strlen(str);
    for(int i = 0; i < len; i++){
      int c = str[i] - 'a';
      if(!temp[tot][c]) temp[tot][c] = ++cnt;
      tot = temp[tot][c];
    }
    //colo[tot]++;
}
void init()
{
    mem(temp,0);
    cnt = 0;
}
int query(int Max){return 2 * cnt - Max + n;}
int main()
{
    while(~scanf("%d",&n)){
      char str[55];
      int Max = -1;
      init();
      for(int i = 0; i < n; i++){
        scanf("%s",str);
        int len = strlen(str);
        Max = max(Max,len);
        buildTree(str);
      }
      printf("%d\n",query(Max));

    }


    return 0;
}

 

無語了,數組開大一點就MLE。1e6 和 5e5這種也扣內存。。。。。。行吧,還是題刷少了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章