HDU 3460 Ancient Printer(字典树)

来源:互联网 发布:斗牛牌型算法 编辑:程序博客网 时间:2024/06/01 07:37

Ancient Printer

Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 1 Accepted Submission(s) : 1
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[/hint]

Author
iSea @ WHU

Source
2010 ACM-ICPC Multi-University Training Contest(3)——Host by WHU
题意:打印字符串,统计需要输入字符个数以及打印的最少操作次数,对于题中案例可知,题求的是最少操作次数,所以对于一些字符串前缀相同的,删除一些字符在输入一些字符的操作是比较少的,由此联想到字典树。
题解:对输入的字符串建立字典树,用 x 标记字典树中经过节点的个数,即经过该点的字符串的个数,用y标记字典树中该节点是否访问过。对于都经过的节点统计一次就好。

#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>using namespace std;struct node{    int x,y;    node *next[26];    node()    {        x=1;        y=0;        for(int i=0;i<26;i++)            next[i]=NULL;    }};node *root;int n,num,ii;char a[10010][60];void buildtree(int t)//建立字典树{    int len=strlen(a[t]);    node *p=root;    for(int i=0;i<len;i++)    {        if(p->next[a[t][i]-'a']==NULL)            p->next[a[t][i]-'a']=new node();        else            p->next[a[t][i]-'a']->x++;        p=p->next[a[t][i]-'a'];    }}void findtree(int t){    int len=strlen(a[t]);    node *p=root;    for(int i=0;i<len;i++)    {        if(p->next[a[t][i]-'a']->y==0)//对于都经过的节点输出时间只需要打印一次        {            num++;            p->next[a[t][i]-'a']->y=1;        }        p->next[a[t][i]-'a']->x--;//打印一次,经过这个节点的字符串减少一个        if(p->next[a[t][i]-'a']->x==0&&(t!=ii))//删除操作,最长的那个不需要删除操作            num++;        p=p->next[a[t][i]-'a'];    }    num++;//打印操作}int main(){    while(scanf("%d",&n)!=EOF)    {        num=0;        root=new node();        int maxx=-1;        ii=0;        for(int i=0;i<n;i++)        {            scanf("%s",a[i]);            buildtree(i);            int len=strlen(a[i]);            if(maxx<len)                maxx=len,ii=i;        }        for(int i=0;i<n;i++)        {            if(i!=ii)            {                findtree(i);            }        }        findtree(ii);        printf("%d\n",num);    }    return 0;}
原创粉丝点击