TC SRM 682 DNASequence

水题,给你一个string,找出只由ACGT四个字符组成的最长连续子串的长度

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
class DNASequence
{
public:
	int longestDNASequence(string str)
	{
		bool isDNA[55];
		memset(isDNA, 0, sizeof(isDNA));
		int l = str.length(), ans = 0, t = 0;
		for (int i = 0; i < l; ++i)
			if (str[i] == 'A' || str[i] == 'C' || str[i] == 'G' || str[i] == 'T')
				isDNA[i] = 1;
		for (int i = 0; i < l; ++i)
		{
			if (isDNA[i])
			{
				++t; ++i;
				for (; i < l; ++i)
				{
					if (isDNA[i])
						++t;
					else
					{
						ans = t > ans ? t : ans;
						t = 0;
						break;
					}
				}
			}
		}
		ans = t > ans ? t : ans;
		return ans;
	}
};

你可能感兴趣的:(TC SRM 682 DNASequence)