标签:长度 str show include 记录 字符 blog 常用 0ms
在数据压缩中,一个常用的途径是行程长度压缩。对于一个待压缩的字符串而言,我们可以依次记录每个字符及重复的次数。这种压缩,对于相邻数据重复较多的情况比较有效。 例如,如果待压缩串为"AAABBBBCBB",则压缩的结果是(A,3)(B,4)(C,1)(B,2)。当然,如果相邻字符重复情况较少,则压缩效率就较低。
现要求根据输入的字符串,得到大小写不敏感压缩后的结果(即所有小写字母均视为相应的大写字母)。
aAABBbBCCCaaaaa
(A,3)(B,4)(C,3)(A,5)
代碼實現:
1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 int n,l; 5 char ch[3000],cn,cu; 6 int main(){ 7 scanf("%s",&ch); 8 l=strlen(ch); 9 for(int i=0;i<l;){ 10 cn=cu=ch[i];n=0; 11 if(cn<‘a‘) cn+=32; 12 else cu-=32; 13 while(ch[i]==cn||ch[i]==cu){i++;n++;} 14 printf("(%c,%d)",cu,n); 15 } 16 printf("\n"); 17 return 0; 18 }
。。。
标签:长度 str show include 记录 字符 blog 常用 0ms
原文地址:http://www.cnblogs.com/J-william/p/6158225.html