小言_互联网的博客

C++笔试汇总(10)-----华为面试

512人阅读  评论(0)

输入:随机子串,输出最长无重复英文子串
例 输入 123DABC123BCASD
输出 BCASD

#include<iostream>
#include<string>
using namespace std;
int main()
{
	char a[] = "123DABC123BCASD";
	char *ptr_start = &a[0];
	char *ptr = &a[0];
	int num = 0;
	int max = 0;
	int start = 0;
	int count = (sizeof(a) / sizeof(char))  - 1;
	for (int i = 0; i < count; i++)
	{
		if (a[i] >= 'A' && a[i] <= 'Z')
		{
			num++;
			if (num > max)
			{
				max = num;//最长子串长度
				start = i - num + 1;//不断更新最长子串的初始位置
				*ptr_start = *(ptr + start);//更新指向初始位置的指针
			}
		}
		else
		{
			
			num = 0;
		}
	}
	for (int i = start; i < start + max; i++)
	{
		cout << a[i];
	}
	system("pause");
}

转载:https://blog.csdn.net/qq_41679776/article/details/102090884
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场