1 [0] 1个样式计数

Description:

描述:

This article provides solution to an algorithm problem based on pattern searching came in the coding round of Samsung.

本文为Samsung编码回合中基于模式搜索的算法问题提供了解决方案。

Problem statement:

问题陈述:

Given a binary string, write an algorithm to find the number of patterns of form 1[0]1 where [0] represents any number of zeroes (minimum requirement is one 0) there should not be any other character except 0 in the [0] sequence.

给定一个二进制字符串,编写一种算法来查找形式为1 [0] 1的模式数量,其中[0]代表任意数量的零(最低要求为0),[0]中不应有任何其他字符(除了0) ] 序列。

Solution

Algorithm:

算法:

  1. Set l = length of string & initialize count to 0.

    设置l =字符串的长度并将初始化计数设置为0。

  2. Find the number of 1's in the string. If all entry is 1 then no such pattern exists for the string input.

    在字符串中找到1的数目。 如果所有条目均为1,则字符串输入不存在这种模式。

  3. Find first '1' for a 1[0]1 pattern

    查找1 [0] 1模式的第一个“ 1”

    While (

    虽然(

    String [element]!= '1')

    字符串[element]!='1' )

    Go to next element.

    转到下一个元素。

  4. Check if the next element of the string is '0' or not.

    检查字符串的下一个元素是否为“ 0”。

  5. If the next element is '0'

    如果下一个元素为“ 0”

    Continue checking next element until the substring of 0 finishes. ([0] part in the pattern)

    继续检查下一个元素,直到子字符串0结束。 (模式中的[0]部分)

  6. Once finished check the next element. If it is '1' increase count.

    完成后,检查下一个元素。 如果为“ 1”,则增加计数 。

    In the next iteration this '1' element is going to be starting element of next 1[0]1 pattern.

    在下一次迭代中,此“ 1”元素将成为下一个1 [0] 1模式的起始元素。

  7. Continue steps 3 to 6 until string length finishes for processing.

    继续执行步骤3到6,直到字符串长度完成处理。

  8. Return count.

    返回计数 。

1 [0] 1模式计数的C ++实现 (C++ implementation for 1[0]1 Pattern Count )

#include <bits/stdc++.h>
using namespace std;

int my(string s){
	//set variables
	int l=s.length(),count=0;
	for(int i=0;i<l;i++){
		//count no of 1's
		if(s[i]!='0') 
			count++;
	}
	//if no of 1=total length then no 1[0]1 pattern
	if(count==l) 
		return 0;
	
	int i=0,flag=0,start=0,end=1;
	count=0;

	while(i<l){
		//go to first '1' of a pattern
		while(s[i]!='1'){ 
			i++;
		}
		//first 1 is found,check the next
		i=i+1;
		//if next is a 0
		if(s[i]=='0'){ 
			//procedd till elements are 0
			while(s[i]=='0')
				i++;
			//if the element after end of 0 substring is 1
			if(s[i]=='1'){
				//pattern is found & increase count
				count++;
			}
		}
	}
	return count; //return no of pattern
}

int main()
{  

	cout<<"program to find no of 1[0]1 patterns in string"<<endl;
	string s;
	cout<<"enter string"<<endl;  
	cin>>s;
	int k=my(s);
	cout<<k<<endl;    

	return 0;
}

Output

输出量

First run:
program to find no of 1[0]1 patterns in string 
enter string 
001010100001101
4

Second run:
program to find no of 1[0]1 patterns in string 
enter string 
111111111111111111111110 
0


翻译自: https://www.includehelp.com/icp/101-pattern-count.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值