| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 637 人关注过本帖
标题: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 回复次数:4 
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
wfpb
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:2188
专家分:0
注 册:2006-4-2
收藏
得分:0 
实在想不明白如何才能在不遍历整个串之前得知0的个数,了不起只能判断1的个数,当1的个数到达n-n/2-1时停止遍历,得出0的个数。但是意义也不大。
所以还请你明示!

[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2007-09-15 17:39
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
Adicelanton
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2007-9-15
收藏
得分:0 
may be you can change the string type to the bitset type and then you could transfer the operation of the bitset type ,just like that:
size_t num=bitvec.cout()//the bitvec is a object of bitset type and the number means that the 1's units;

another way :you can make a use of the iterator to count the 1's units in you string ,for exemple :

#include<iostream>
#include<string>
#include<cstddef>
using namespace std;
int main(){
size_t num0=0,num1=0;
string str("0001111000");
string::iterator iter;
for(iter=str.begin();iter!=str.end();++iter){
if(*iter!=' '){
if(*iter=='1')
num1++;
if(*iter=='0')
num0++;
}
else cout<<"errer!"<<endl;
}
cout<<num1<<endl;
cout<<num0<<endl;
return 0;
}
if you have some problems please show me and give me a reason ,thank you.
by the way ,i see ,i can't make sure that i understand you real idea.

如果你对我报有仇恨,我将背负它,接受它! 我不知道风在向哪一个方向吹,我知道,你有你的,我有我的方向.
2007-09-15 22:04
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.023211 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved