HUST 1004 String Compare(字符串前缀对)
来源:互联网 发布:java构造函数的特点 编辑:程序博客网 时间:2024/06/05 18:41
题目链接:http://acm.hust.edu.cn/problem.php?id=1004
Maybe there are 750,000 words in English and some words are prefix of other words, for example: the word "acm" can be treat as one prefix of "acmicpc". What's more, most of such pairs of words have relationship between them. Now give you a dictionary, your work is to tell me how many such pairs.
题目意思很简单,开始一看,想都没想就trie树,后来MLE了才改成现在的代码
首先排序,然后前缀关系一定相邻,那么从当前位置向后找,找的时候用二分
排序一遍,遍历+二分一遍
n*logn*30
#include <string.h>#include <algorithm>#include <stdio.h>#include <iostream>using namespace std;struct point{ char str[31]; int len;}po[51000];int n;long long ans;bool cmp(const point &a,const point &b){ return strcmp(a.str,b.str)<0;}bool is_ok(char *first,char *second,char num){ for(int i=0;i<num;i++) if(first[i]!=second[i]) return false; return true;}int upbound(int pos,int left,int right){ int mid,ans=0; while(left <= right){ mid=(left+right)>>1; if(po[pos].len > po[mid].len){ right=mid-1; continue; } if(is_ok(po[pos].str,po[mid].str,po[pos].len)){ left=mid+1; ans=max(mid,ans); } else right=mid-1; } return ans;}int main(){ int i,j,k,t,now; scanf("%d",&t); while(t--){ scanf("%d",&n); ans=0; for(i=0;i<n;i++){ scanf("%s",po[i].str); po[i].len=strlen(po[i].str); } sort(po,po+n,cmp); for(i=0;i<n;i++){ now=upbound(i,i,n-1); ans+=(now-i); } if(ans > 11519) ans%=11519; printf("%lld\n",ans); } return 0;}
0 0
- HUST 1004 String Compare(字符串前缀对)
- hust 1004 - String Compare 字符串排序
- HDU1894 String Compare 字符串比较
- 1004 String compare
- 用 Compare 函数对字符串进行比较
- hdu5311 Hidden String dp找字符串前缀
- Hust oj 2106 Genuine Messages(字符串)
- Hust oj 1177 Time Transform(字符串)
- c++ STL::String::Compare (_Buf内容一致,compare不相等)
- HUST 1328 String KMP
- 【hust 1328】 - String
- C#中字符串的一些常见函数,比如string.Compare()
- HDU 1894 String Compare (排序)
- String..::.Compare Method (String, String)
- hdu1894 String Compare(string判断子串)
- Hust oj 1109 店长终极推荐(字符串)
- Hust oj 1497 Death Knight Hero(字符串水题)
- 前缀字符串
- c:forEach>用法
- 使用events DUMP buffer cache中指定的数据块
- 三星Note3使用技巧 “随心剪贴”功能怎么用
- 《视频解密》中文版(第四版) 第六章 数字视频接口(第二部分)
- java中子类与子类型的区别
- HUST 1004 String Compare(字符串前缀对)
- android Fragments详解三:实现Fragment的界面
- linux下的daemon进程
- what's the difference between carry out/conduct/execute/implement/perform?
- 读取excel到datatable 读去txt文件datatable 写入txt
- Oracle常用的Sql
- ORACLE中的各种SCN查询
- 三星Note3使用技巧:怎么使用随笔窗口
- PagedDataSource类 实现DataList控件 分页