[Algorithm] Problem number 7

Threads: link
post code: VOSSEVEN
Given the string of N characters, Each character is a digit from 0 to 9
Request: For each segment the number 7 consecutive see the child count how many times it appears in the string.

Input

Banana

Output

Each line contains a corresponding length from low to high and numbers of its occurrence. String data to ensure that at least 1 number 7. If the number of occurrences by 0 What they do not print.
Example
Input: 72774777 Output:
1 6
2 3
3 1
Limit:

  • 30% Some tests have N <= 10^3.
  • 30% Some tests have N <= 10^5.
  • In all tests N <= 10^6.

* call f[in] is the only strings of numbers 7 continuous and has a length of i.
VD with chain 72774777 then f[1] = 1, f[2] = 1, f[3] = 1.
* call ans[in] the length of the strings that appear on the string s i. Easy to see if the string length is i will create 1 string length i, 2 string length (i-1), … , if f[in] string length f i will be out[in] string length i, f[in]*(i-j+1) j length strings.
MD f[1] = 1 there 1 string length 1.
f[2] = 1 there 1 string length 2, f[2]*(2-1+1) = 2 string length 1.
f[3] = 1 there 1 string length 3, f[3]*(3-2+1) = 2 string length 2, f[3]*(3-1+1) = 3 string length 1.
-> Browse round i, if f[in]>0:
-> years[j]= years[j]+f[in]*(i-j+1), with j<=i. Cuối cùng là in ra các giá trị i mà ans[i]>0.

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int main(){
	string s;
	cin >> s;
	int n = s.length();
	int *f = new int[n + 1];
	int *ans = new int[n + 1];
	
	for (int i=0; i<=n; i++){
		f[i] = ans[i] = 0;
	}
	// duyet tinh f[i]
	for (int i=0; i<n; i++){
		int c = 0;
		while(i < n && s[i] == '7'){
			c++;
			i++;
		}
		if (c > 0){
			f++;
		}
	}
	/*
	for (int i=1; i<=n; i++){
		cout << f[i] << " ";
	}
	cout << endl;
	*/
	
	// duyet tinh ans[i]
	for (int i=n; i>0; i--){
		if (f[i] > 0){
			for (int j=0; j<=i; j++){
				ans[j] += f[i]*(i-j+1);
			}
		}
	}
	
	// duyet in ra ket qua
	for (int i=1; i<=n; i++){
		if (ans[i] > 0){
			printf("%d %d\n", i, ans[i]);
		}
	}
	//cout << endl;
	
	return 0;
}

In the above code you should note that the results should be used when printf, if used by cout, then down the line “\n” endl will slow because so many than “\n”, This place has to stick his delegation. Thankfully, after consultation with ace earned on VNOI