C程序,用于字符串中最长运行的长度 [英] C program for length of the longest run in the string

查看:89
本文介绍了C程序,用于字符串中最长运行的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在字符串中,run是一个子字符串,包含相同字符的连续

次出现。例如,字符串

mississippi包含以下运行 - ss,ss和pp。



In这个问题,给定一个字符串,你必须在字符串中输出

最长的长度。



输入

-----

一个字符串,长度最多为100.字符串保证至少有一个
运行。



输出

------

字符串中运行时间最长的长度。



样本输入

------------

abbaaacccc



样本输出

-------------

4



我尝试过:



#include< stdlib.h>

#include< stdio.h>

#include< string.h>

#define NO_OF_CHARS 256



int min(int a,int b);



int longestUniqueSubsttr(char * str)

{

int n = strlen(str);

int cur_len = 1; //当前子串的长度

int max_len = 1; //结果

int prev_index; //上一个索引

int i;

int * visited =(int *)malloc(sizeof(int)* NO_OF_CHARS);



/ *将访问数组初始化为-1,-1用于

表示尚未访问该字符。 * /

for(i = 0; i< NO_OF_CHARS; i ++)

visit [i] = -1;



/ *通过存储索引数据中第一个字符的索引

来标记访问的第一个字符。 * /

访问过[str [0]] = 0;



/ *从第二个字符开始。第一个字符是

已经处理(cur_len和max_len初始化

为1,访问[str [0]]设置为* /

for(i = 1; i< n; i ++)

{

prev_index = visit [str [i]];



/ *如果

已经处理过的子字符串中没有currentt字符,或者它不是当前NRCS的

的一部分,那么执行cur_len ++ * /

if(prev_index == -1 || i - cur_len> prev_index)

cur_len ++;



/ *如果当前字符在当前

中被认为是NRCS,则更新NRCS以从前一个实例的下一个字符开始

。* /

其他

{

/ *此外,当我们更改NRCS时,我们还要检查是否b / b的长度/>
p相反,NRCS大于max_len或

不是。* /

if(cur_len> max_len)

max_len = cur_len;



cur_len = i - prev_index;

}



//更新当前字符的索引

访问过[str [i]] = i;

}



//比较最后一个NRCS与max_len的长度和

//如果需要更新max_len

if(cur_len> max_len)

max_len = cur_len;



免费(已访问); //为访问分配的可用内存

返回max_len;

}



/ *获取的效用函数最少两个整数* /

int min(int a,int b)

{

return(a> b)?b: a;

}



/ *用于测试上述功能的驱动程序* /

int main()< br $>
{

char str [] =ABDEFGABEF;

printf(输入字符串是%sn,str);

int len = longestUniqueSubsttr(str);

printf(最长非重复的长度

字符子串是%d,len );

返回0;

}

In a string, a "run" is a substring with consisting of consecutive
occurrences of the same character. For example, the string
"mississippi" contains the following runs - "ss", "ss" and "pp".

In this question, given a string, you have to output the length of the
longest run in the string.


Input
-----
A string, having length at most 100. The string is guaranteed to have
at least one run.

Output
------
The length of the longest run in the string.

Sample Input
------------
abbaaacccc

Sample Output
-------------
4

What I have tried:

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define NO_OF_CHARS 256

int min(int a, int b);

int longestUniqueSubsttr(char *str)
{
int n = strlen(str);
int cur_len = 1; // lenght of current substring
int max_len = 1; // result
int prev_index; // previous index
int i;
int *visited = (int *)malloc(sizeof(int)*NO_OF_CHARS);

/* Initialize the visited array as -1, -1 is used to
indicate that character has not been visited yet. */
for (i = 0; i < NO_OF_CHARS; i++)
visited[i] = -1;

/* Mark first character as visited by storing the index
of first character in visited array. */
visited[str[0]] = 0;

/* Start from the second character. First character is
already processed (cur_len and max_len are initialized
as 1, and visited[str[0]] is set */
for (i = 1; i < n; i++)
{
prev_index = visited[str[i]];

/* If the currentt character is not present in the
already processed substring or it is not part of
the current NRCS, then do cur_len++ */
if (prev_index == -1 || i - cur_len > prev_index)
cur_len++;

/* If the current character is present in currently
considered NRCS, then update NRCS to start from
the next character of previous instance. */
else
{
/* Also, when we are changing the NRCS, we
should also check whether length of the
previous NRCS was greater than max_len or
not.*/
if (cur_len > max_len)
max_len = cur_len;

cur_len = i - prev_index;
}

// update the index of current character
visited[str[i]] = i;
}

// Compare the length of last NRCS with max_len and
// update max_len if needed
if (cur_len > max_len)
max_len = cur_len;

free(visited); // free memory allocated for visited
return max_len;
}

/* A utility function to get the minimum of two integers */
int min(int a, int b)
{
return (a>b)?b:a;
}

/* Driver program to test above function */
int main()
{
char str[] = "ABDEFGABEF";
printf("The input string is %s n", str);
int len = longestUniqueSubsttr(str);
printf("The length of the longest non-repeating "
"character substring is %d", len);
return 0;
}

推荐答案

为什么这么复杂?
我知道这是家庭作业,但是......这不是一项复杂的任务:你需要做的就是找到最长的跑步长度并打印出来。



所以:

1)创建一个变量来保存当前的运行长度。将其初始化为0

2)创建一个变量以保持最长的运行长度。将其初始化为-1

3)检查字符串长度:如果是1或0,那就是运行长度。

4)循环字符串,从第二个角色。它和它之前的一样吗?

4.1)是的。将当前运行长度增加1。这是否大于最长的运行长度?

4.1.1)是的。设置最长为当前。

4.1.2)号码什么也不做。

4.2)否。将当前运行长度设置为1。

5)重复4直到字符串结束。

6)打印最长的运行长度。



简单:它可能比描述更少的C代码!
Why are you complicating this so much?
I know it's home work, but ... this isn't a complicated task: all you need to do is find the longest run length and print it.

So:
1) Create a variable to hold the current run length. Initialize it to 0
2) Create a variable to hold the longest run length. Initialize it to -1
3) Check the string length: if it is one or zero, that's the run length.
4) Loop through the string, starting with the second character. Is it the same as the one before it?
4.1) Yes. Increment the current run length by one. Is that larger than the longest run length?
4.1.1) Yes. Set the longest to the current.
4.1.2) No. do nothing.
4.2) No. Set the current run length to one.
5) Repeat from 4 until the end of the string.
6) Print the longest run length.

Easy: it's probably less C code than description!


#include<stdio.h>
int main()
{
	char str[10];
	int i,j,k,len=0;
        printf("Enter the string:\n");
	scanf("%s",str);
	for(i=0,k=0;str[i]!='\0';i++,k++)
	{
		if(str[i]!=str[i+1])
		{
			if(k>len)
			 len=k;
			k=0;
		}
	}
	printf("%d",len);
	return 0;
}


您的代码中的问题是它与此作业无关。



我们不做你的HomeWork。

HomeWork不会测试你乞求别人做你的工作的技巧,它会让你思考并帮助老师检查您对所学课程的理解以及您应用这些课程时遇到的问题。

你的任何失败都会帮助你的老师发现你的弱点并设定补救措施。

你的任何失败都会帮助你了解什么有效,什么无效,被称为'试错'学习。

所以,试一试,重读课程并开始工作。如果您遇到特定问题,请显示您的代码并解释这个问题,我们可能会提供帮助。



作为程序员,您的工作是创建算法解决特定问题,你不能依赖别人永远为你做,所以有一段时间你必须学会​​如何。越快越好。
The problem in your code is that it is not related to this homework.

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.


这篇关于C程序,用于字符串中最长运行的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆