//Code for Character Count
#include <iostream>
#include <string.h>
#include <vector>
std::vector <char> s_character;
std::vector <int> count_occurence;
/*Function to check occurence of a character in the character vector,
if found return position,
else return -1 indicating no occurence
*/
int check_character_occurence(char character)
{
for (int i=0;i<s_character.size();i++)
{
if(s_character.at(i)==character)
return i;
}
return -1;
}//end_of_check_character_occurence_function
/*Function to do the counting of individual characters,
if character is not present(occuring for the first time) then add to both character vector and count vector
else update count at position
*/
void count_algorithm(char character)
{
int pos_flag;
pos_flag = check_character_occurence(character);
if (pos_flag==-1)
{
s_character.push_back(character);
count_occurence.push_back(1);
}
else
count_occurence.at(pos_flag)++;
}//end_of_count_algorithm_function
int main()
{
std::string sequence;
char separated_character;
std::cout<<"\nEnter String: ";
std::cin>>sequence;
std::cout<<"\nLength is "<<sequence.length()<<" characters.";
for(int i=0; i<sequence.length(); i++)
{
separated_character=sequence[i];
count_algorithm(separated_character);
}
for(int i=0;i < s_character.size(); i++)
std::cout<<"\nCharacter: "<<s_character[i]<<" Occurence: "<<count_occurence[i];
return 0;
}//end_of_main_code
私はDNA配列サンプルを取った。文字カウントのコードを書いています。このC++コードを最適化する方法はありますか(時間、空間の複雑さなど)
出力:
は、文字列を入力します。AGCTAGCATCGTGTCGCCCGTCTAGCATACGCATGATCGACTGTCAGCTAGTCAGACTAGTCGATCGATGTG
長は72文字です。
キャラクター:出現:16
キャラクター:G出現:19
キャラクター:C出現:19
キャラクター:T出現:18
あなたが解決しようとしている問題は何ですか? –
あなたの研究/デバッグの努力をこれまでのところ表示してください。まず[Ask]ページをお読みください。 –
私はそれがより速く実行できるかどうかを意味します。 –