========劳驾one_add_one========

babysloth 2001-08-20 10:15:41
有个程序,我用来测试STL性能的。
请您帮忙在速度慢一些的机器上测试速度一下,好吗?
需要符合C++标准的编译器。

#include <fstream>
#include <algorithm>
#include <functional>
#include <string>
#include <vector>
using namespace std;

int DNA(const string& virus, const string& life)
{
const string d_virus = virus + virus;
const unsigned int len = virus.size();
if(len >= life.size()&&d_virus.find(life) != string::npos)
return life.size();
string::size_type pos, start;
unsigned max_len = 0;

string::const_iterator i, j = d_virus.begin() + len, e;
pair<string::const_iterator, string::const_iterator> mis;
vector<int> skip;
vector<int>::iterator vi, pi, ei;
skip.resize(len, 0);

for(unsigned c = 1; c <= len; ++c)
{
if(c < len&&c <= max_len)continue;
for(i = d_virus.begin(); i != j; ++i)
{

vi = skip.begin() + (i - d_virus.begin());
ei = (skip.end() - vi > (int)c)? vi + c: skip.end();
for(pi = vi;; ++pi)
{
pi = find_if(pi, ei, bind1st(less<int>(), 0));
if(pi == ei)break;
if(c - *pi >= unsigned(pi - vi))break;
}
if(pi != ei)
{
i += (pi - vi);
continue;
}

pos = life.find(string(i, i + c));
if(pos == string::npos)
{
if(*vi == 0)*vi = c;
continue;
}
if(c < len){max_len = c; break;}

start = pos;
pos += c;
do{
e = (pos + c <= life.size())
? (life.begin() + pos + c): life.end();
mis = mismatch(life.begin() + pos, e, i);
pos = mis.first - life.begin();

}while(mis.first == e&&e != life.end());
max_len = max(max_len, pos - start);

if(max_len == life.size())break;
}
}
return max_len;
}

void main(int argc, char* argv[])
{
const char* const file = (argc > 1) ? argv[1]: "virus.in";
ifstream in(file);
int t;
string virus, life;
in >> t;
virus.reserve(t);
in >> virus >> t;
life.reserve(t);
in >> life;
cout<<DNA(virus, life)<<endl;
}

...全文
68 点赞 收藏 7
写回复
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
goldcattle 2001-08-21
好福气
回复
babysloth 2001-08-21
什么黑名单啊?
回复
yug 2001-08-21
to one_add_one()快给分给分:
小心啊!别上了黑名单,没看CSDN的公告吗?
呵呵,千万不要贪心!
回复
one_add_one 2001-08-20
哇,这样多分呀,刚才我CSDN一直打不开,现在终于打开了!

我发了,哈哈哈!


多谢多谢!
回复
frman 2001-08-20
哇哇哇~~~~~太夸张了~~~~~5555555我的P133要还在就好了:(((((
回复
babysloth 2001-08-20
嘿嘿,与人方便,自己方便嘛。
回复
starfish 2001-08-20
这么多分呀,1+1这下要乐歪了:)
回复
发动态
发帖子
数据结构与算法
创建于2007-08-27

3.2w+

社区成员

数据结构与算法相关内容讨论专区
申请成为版主
社区公告
暂无公告