Find the maximal product of string prefixes

Task description
A prefix of a string S is any leading contiguous part of S. For example, "c" and "cod" are prefixes of the string "codility". For simplicity, we require prefixes to be non-empty.
The product of prefix P of string S is the number of occurrences of P multiplied by the length of P. More precisely, if prefix P consists of K characters and P occurs exactly T times in S, then the product equals K * T.
For example, S = "abababa" has the following prefixes:
"a", whose product equals 1 * 4 = 4,
"ab", whose product equals 2 * 3 = 6,
"aba", whose product equals 3 * 3 = 9,
"abab", whose product equals 4 * 2 = 8,
"ababa", whose product equals 5 * 2 = 10,
"ababab", whose product equals 6 * 1 = 6,
"abababa", whose product equals 7 * 1 = 7.
The longest prefix is identical to the original string. The goal is to choose such a prefix as maximizes the value of the product. In above example the maximal product is 10.
In this problem we consider only strings that consist of lower-case English letters (a−z).
Write a function
class Solution { public int solution(String S); }
that, given a string S consisting of N characters, returns the maximal product of any prefix of the given string. If the product is greater than 1,000,000,000 the function should return 1,000,000,000.
For example, for a string:
S = "abababa" the function should return 10, as explained above,
S = "aaa" the function should return 4, as the product of the prefix "aa" is maximal.
Assume that:
N is an integer within the range [1..300,000];
string S consists only of lower-case letters (a−z).
Complexity:
expected worst-case time complexity is O(N);
expected worst-case space complexity is O(N) (not counting the storage required for input arguments).
Copyright 2009–2013 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

下面的解法只能得到结果,而没有达到题目要求的复杂度、没有考虑计算范围。

//头文件
#include<stdio.h>
#include<string.h>
#define N 80
int fun( char * str, char *zi);

//主函数

#include"hhh.h"
int main(void)
{
    char p[]="abababa";
    char s1[N][N];  //   char s1[N][N]={'\0'};
    int i,temp=0;
    int len=strlen(p);
    int max=0;


    for(i=0;i<len;i++)
    {
        strncpy(s1[i],p,i+1);
        printf("%d\t%s\n",i+1,s1[i]);

        temp=fun(p, s1[i]);
        puts("");
        if(max<temp*(i+1))
            max=temp*(i+1);
    }
    printf("max=%d",max);

    return 0;
}

//调用函数

#include"hhh.h"
int fun(char *str, char *zi)
{
	//printf("子函数中 %s  %s\n",str,zi);
	int count=0;
	int i=0,j=0;
	int t=0;
	for(i=0;str[i];i++)
	{
		t=i;
		for(j=0;zi[j];)
		{
			if(str[t] != zi[j])
				break;       //一定要有break!!!容易疏忽的地方
			else
			{
				t++;j++;
			}
		}
		if(zi[j]=='\0')
			count++;
	}
	printf("%s中含有%s %d 个\n",str,zi,count);
	return count;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值