| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 636 人关注过本帖
标题:Problem: count how many 0's in a string consisting of 0 and 1 only
取消只看楼主 加入收藏
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
 问题点数:0 回复次数:2 
Problem: count how many 0's in a string consisting of 0 and 1 only

Problem statement:

I have a string consisting of 0 and 1's only, say "000100101100". I want to count how many 0's are there in the string "efficiently".

Let n = length of the string. Then obviously we have O(n) algorithm.

My string will have more 0's than 1's; i.e., at least n/2+1 chars will be 0's.

My question is:

Can we do better, say O(lg n)?

搜索更多相关主题的帖子: Problem count string consisting 
2007-09-14 19:04
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 
No, I don't know if it is possible to achieve O(lg n) running time. I am just asking for help.


I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-09-15 20:55
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 
thanks for your idea about bitset.

I did consider the bitset template for a while. But then I found it is not appropriate for my problem:

I want to use a long string s of length 10 million to represent the prime numbers less than 10 million.

You see that the string has lots of 0's (s[i]=='0' if i is not prime, otherwise, s[i]== '1').

Even if you can use bitset, I don't know how efficient the count() function of bitset is. My guess is that it would be O(n).

I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-09-16 15:00
快速回复:Problem: count how many 0's in a string consisting of 0 and 1 onl ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017024 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved