POJ 2001 Shortest Prefixes
2018-06-17 22:17:15来源:未知 阅读 ()
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 18684 | Accepted: 8086 |
Description
In the sample input below, "carbohydrate" can be abbreviated to "carboh", but it cannot be abbreviated to "carbo" (or anything shorter) because there are other words in the list that begin with "carbo".
An exact match will override a prefix match. For example, the prefix "car" matches the given word "car" exactly. Therefore, it is understood without ambiguity that "car" is an abbreviation for "car" , not for "carriage" or any of the other words in the list that begins with "car".
Input
Output
Sample Input
carbohydrate cart carburetor caramel caribou carbonic cartilage carbon carriage carton car carbonate
Sample Output
carbohydrate carboh cart cart carburetor carbu caramel cara caribou cari carbonic carboni cartilage carti carbon carbon carriage carr carton carto car car carbonate carbona
Source
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<queue> 6 #include<algorithm> 7 #include<cstdlib> 8 using namespace std; 9 const int MAXN=100001; 10 void read(int &n) 11 { 12 char c='+';int x=0,flag=1; 13 while(c<'0'||c>'9') 14 {c=getchar();if(c=='-')flag=-1;} 15 while(c>='0'&&c<='9') 16 {x=x*10+c-48;c=getchar();} 17 n=(x*flag); 18 } 19 struct TRIE 20 { 21 int ch[MAXN][51]; 22 int sz; 23 int val[MAXN]; 24 TRIE() 25 {memset(ch[0],0,sizeof(ch[0])); 26 memset(val,0,sizeof(val)); 27 sz=1;} 28 int idx(char c) 29 { 30 return c-'a'; 31 } 32 void Insert(char *s) 33 { 34 int l=strlen(s);int now=0; 35 for(int i=0;i<l;i++) 36 { 37 char c=idx(s[i]); 38 if(!ch[now][c]) 39 { 40 memset(ch[sz],0,sizeof(ch[sz])); 41 ch[now][c]=sz++; 42 } 43 now=ch[now][c]; 44 val[now]++; 45 //printf("%d ",val[now]); 46 } 47 } 48 void query(char *s) 49 { 50 int l=strlen(s); 51 int now=0; 52 for(int i=0;i<l;i++) 53 { 54 int c=idx(s[i]); 55 now=ch[now][c]; 56 printf("%c",s[i]); 57 if(val[now]==1) 58 return ; 59 } 60 } 61 }trie; 62 char s[MAXN][201]; 63 int n=1; 64 65 int main() 66 { 67 68 while(scanf("%s",s[n])==1) 69 { 70 n++; 71 trie.Insert(s[n-1]); 72 } 73 for(int i=1;i<n;i++) 74 { 75 printf("%s ",s[i]); 76 trie.query(s[i]); 77 printf("\n"); 78 } 79 return 0; 80 }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:#110. 乘法逆元
- POJ-3278 2020-04-01
- Asteroids!_poj2225 2020-02-09
- poj-1753题题解思路 2020-01-26
- POJ1852 2019-11-11
- POJ2431 优先队列+贪心 - biaobiao88 2019-11-03
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash