pattern search code help

Hi,

could someone help me finding a way to do the following:

if I have a pattern like 0100101000

I want the out put like 00100010000

its like the second container always ones the consecutive position after a one is detectd in the first, but if a zero is trapped in between 2 ones like 101 it becomes 010

Re-explain the pattern? I only understood that you want to replace all instances of 101 with 010, but there's another aspect to it since there's an extra 0 at the front.


its like the second container always ones the consecutive position after a one is detectd in the first

I assume this explains that first pattern the output is supposed to follow, but I have no idea what you're trying to say.
senseless
In the absence of any meaningful description of the problem ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
using namespace std;

string turqo( const string &input )
{
   string output;
   for ( char c : input )
   {
      if ( c == '0' ) output += '0';
      else if ( output.size() < 2 || output.substr( output.size() - 2 ) != "10" ) output += "01";
   }
   return output;
}


int main()
{
   string test = "0100101000";
   cout << test << '\n';
   cout << turqo( test ) << '\n';
}


0100101000
00100010000
sorry for bad explain, i change the condition a little bit now

its like I have a sequence e.g 0100101000 of say length n

I want to generate another sequence based on the first

in the second sequence is of same length as n, by default this sequence push "0"

but if it detects a 1 in the [i] th position in the first, it flags 1 in the [i+1] and [i-1] position in
the second

so 0100101000 becomes like
1011010100
turqo wrote:
i change the condition a little bit now


Maybe you would like to state the original problem, not your interpretation of it?
https://en.wikipedia.org/wiki/XY_problem

Anyway:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
using namespace std;

string turqo2( const string &input )
{
   int n = input.size();
   string output( n, '0' );
   for ( int i = 0; i < n; i++ )
   {
      if ( input[i] == '1' )
      {
         if ( i > 0     ) output[i-1] = '1';
         if ( i < n - 1 ) output[i+1] = '1';
      }
   }
   return output;
}


int main()
{
   string test = "0100101000";
   cout << test << '\n';
   cout << turqo2( test ) << '\n';
}


0100101000
1011010100
I think this is almost clear my doubt thanks thanks

can you explain to me this why we need if ( i > 0) if ( i < n - 1 )


why cant I do some thing like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

vector<int> myvector{0,0,1,0,1,0,0} ,    flag_vector{};

for(int i=0; i<myvector.size(); ++i)
       {
       	  if(myvector[i]==1 )
       	  {
			   for(int j=0; j<myvector.size(); ++j) 
			   
			   {  
			      if(j==(i-1)|| j==(i+1))
			   {
			   	  flag_vector[j]=1;		
			   }
			            
			  }		  
				  
		   }
	   }  



You can indeed do something like that ... but it is like scanning all 365 days in a year when only one is your birthday. In other words, highly inefficient. Imagine if your vector was 1 million items long. You can check 2 million items or you can check a million million items - entirely your choice, but I know which I would do.

The purpose of the if tests is simply to go directly to placing the '1', but making sure that I didn't go beyond the end of the array/string.
Topic archived. No new replies allowed.