t*****n 发帖数: 25 | 1 Program a fast and simple solution to the following problem without using
any
regular expression or pattern matching utilities. Given a non-finite stream
of
characters, output an "A" if the characters "xxx" are found in exactly that
sequence. If the characters "xMx" are found instead, output a "B". Do not
re-
process characters so as to output both an “A” and a “B” when processing
the
same input. For example:
1. The following input xxMxMxxxMxxx would produce the following output: BAA
2. The |
r*********r 发帖数: 3195 | 2 #include
#include
using std::cout;
using std::endl;
using std::string;
string process(const string& s)
{
string r;
for(int i=0; i
if( s.substr(i,3) == "xxx" ) {
i += 2;
r += 'A';
}
else if( s.substr(i,3) == "xMx") {
i += 2;
r += 'B';
}
}
return r;
}
int main()
{
cout << process("xxMxMxxxMxxx") << endl
<< process("xxxMxMxxxxMMxMxMx") << endl;
return 0;
} |
s*******n 发帖数: 688 | 3 DNA sequencing?
stream
that
not
processing
【在 t*****n 的大作中提到】 : Program a fast and simple solution to the following problem without using : any : regular expression or pattern matching utilities. Given a non-finite stream : of : characters, output an "A" if the characters "xxx" are found in exactly that : sequence. If the characters "xMx" are found instead, output a "B". Do not : re- : process characters so as to output both an “A” and a “B” when processing : the : same input. For example:
|
t*****n 发帖数: 25 | 4 Good solution!
【在 r*********r 的大作中提到】 : #include : #include : using std::cout; : using std::endl; : using std::string; : string process(const string& s) : { : string r; : for(int i=0; i: if( s.substr(i,3) == "xxx" ) {
|
x*******j 发帖数: 17 | 5 我的答案:用c#的,文不对题。
string mystring ="xxmxxmxmxxxxxxmxxmxxmx%";
// char [] a= new char [3]{'a','a','a'};
//char [] b= new char [3]{'b','b','b'};
//char aa = 'a';
int i=0;
int aflag = 0;
int bflag1 = 0;
int bflag2 = 0;
while (mystring[i] != '%')
{
Console.WriteLine("the string char is :{0}",mystring[i]);
i |
q*c 发帖数: 9453 | 6 有重复。 不等的时候, 已经比较了 2-3 个字符, 但是只
往前移动一个。
应该用状态机,就最高效率。
【在 r*********r 的大作中提到】 : #include : #include : using std::cout; : using std::endl; : using std::string; : string process(const string& s) : { : string r; : for(int i=0; i: if( s.substr(i,3) == "xxx" ) {
|