大写字母出现次数统计问题(帮忙看下)
问题描述:
例如给定一个由大写字母组成的序列
ABCDEFGABCDDD其中ABC分别出现了2次,D出现了4次,EFG分别出现了1次
对于给定的字符序列,求每个大写字符出现的次数,并把出现的次数按从小到大的顺序输出,如出现的次数相同则按字母序列排序输出
对于不是特别大的字母序列,可以用长度为26位数组记录26个字母出现次数,然后遍历的方法输出,但是对于很长的字母序列,似乎那样方法不好吧?
下面是我写的,可能显得麻烦,而且还存在错误,苦于找不到,向各位牛人请求帮忙啊!
我的想法是:出现次数和相应字母用一个结构体存储,对输入序列关于次数从小到大排序后,截取出次数相同的字母集合,调用out函数排序处理输出按字母表顺序的字母序列,并输出该字母集合的出现次数。
程序运行举例:
输入:
ADDAABCBBCECDEDaDEFFGGgGHHHkHHKH
输出:
K:1
F:2
ABCEG:3
D:5
H:6
以下是本人写的code,好像是有点问题,但是我测试了好多数据都找不出来,希望那位帮帮忙吧!先谢过,待解之后给分!
#include <iostream>#include <string>#include <vector>#include <cstdlib>using namespace std;struct W{ char uper;//字母值 int count;//该字母出现次数}; int cmpCnt( const void *a ,const void *b)//给出现次数排序qsort()时候用到的{ return ((W *)a)->count > ((W *)b)->count ? 1 : -1;} void out(int size,vector<char> &nn)//处理次数相同的离散字母按从小到大输出字母序列{ char tt[26+1] = {'#'}; for(int j = 0; j < size; j++) tt[j] = nn.at(j); for(int k = 0; k < size; k++) for(int m = k+1; m < size; m++) if( tt[m] < tt[k] ) { char t = tt[m]; tt[m] = tt[k]; tt[k] = t; } for(int k = 0; k < size; k++) cout<<tt[k]; for(int j = size-1; j >= 0 ; j--) nn.pop_back();}int main(){ W mm[26]; vector<char> nn; for(int i = 0; i < 26; i++) { mm[i].uper = char(i+65); mm[i].count = 0; } string str; cin>>str; const char* pstr = str.c_str(); for(int i = 0; i < (int)str.size(); i++) if( *(pstr+i) >= 'A' && *(pstr+i) <= 'Z' ) { int index = int( *(pstr+i) ) - 65; mm[index].count ++; }; qsort(mm,26,sizeof(mm[0]),cmpCnt); int pc = 0; while( !mm[pc].count ) pc++; if( pc == 26) { cout<<"No Answer"<<endl; return 0; } int old = 65535; for(int i = pc; i < 26; i++) { if( mm[i].count <= old ) { old = mm[i].count; nn.push_back(mm[i].uper); } else{ out(nn.size(),nn); old = mm[i].count; nn.push_back(mm[i].uper); } } out(nn.size(),nn); return 0;}
foreach(currentChar in s)
{
if(currentChar<="A"&¤tChar>="Z")
counter[currentChar-"A"]++;
}
}
public void Output()
{
uint counts="";
uint index=0;
foreach(counts in counter)
writeline((index+++"A").ToString()+":"+counts.ToString());
}
}