A Count Task

Problem Description

Count is one of WNJXYK’s favorite tasks. Recently, he had a very long string and he wondered that how many substrings which contains exactly one kind of lowercase in this long string. But this string is so long that he had kept counting for several days. His friend Kayaking wants to help him, so he turns to you for help.

Input

The input starts with one line contains exactly one positive integer T which is the number of test cases.

Each test case contains one line with a string which you need to do a counting task on.

Output

For each test case, output one line containing “y” where y is the number of target substrings.

Sample Input

3

qwertyuiop

qqwweerrttyyuuiioopp

aaaaaaaaaa

Sample Output

10

30

55

Hint

1<=T<=20,1<=len(string)<=105,1<=∑len(string)<=105

Strings only contain lowercase English letters.

正确代码

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
long long n=0,j=0;
char a[100000];
cin>>n;
while(n--)
{
long long count=1,ret=0;
cin>>a;
long long lena=strlen(a);
for(int i=0;i<lena;i++)
{
if(a[i]==a[i+1])
{
count++; }
else if (a[i]!=a[i+1])
{
ret+=count*(count+1)/2;
count=1;
} }
cout<<ret<<endl; }
return 0;
}

题意理解

输入一个字符串,求它有多少个子字符串只包含一种小写字母。比如aaaa4个a,那么选一个有4种,选两个有3种,选三个有2种,选四个有1种,和为4+3+2+1。先进行一次遍历,统计他相同的小写字母个数,再利用等差数列公式求和。

错误以及调试

这类题型调试可以直观的看出错误点,主要程序是进行相同字符的子串字符个数判断,即

for(int i=0;i<lena;i++)
{
if(a[i]==a[i+1])
{
count++; }
else if (a[i]!=a[i+1])
{
ret+=count*(count+1)/2;
count=0;
} }

因为运行计算错误所以我进行调试

结果是因为count的初始化赋值错误,应该以1初始化,因为程序在两个不相同的字符判断中会自动跳过一次程序使得本应该判断三次的count仅仅++两次,所以,以1初始化就能解决该问题。

最后运行成功截图。

#C++初学记录(判断子串#数学结合)的更多相关文章

  1. #C++初学记录(素数判断2)

    素数判断2 比较简单的算法,没有技术含量 A prime number is a natural number which has exactly two distinct natural numbe ...

  2. #C++初学记录(素数判断)

    练习题目二 素数判断 A prime number is a natural number which has exactly two distinct natural number divisors ...

  3. #C++初学记录(算法4)

    A - Serval and Bus It is raining heavily. But this is the first day for Serval, who just became 3 ye ...

  4. #C++初学记录(sort函数)

    sort函数 前言:当进行贪心算法的学习时,需要用到sort函数,因为初学c++汇编语言,sort的具体用法没有深入学习,所以这里进行sort学习记录并只有基础用法并借用贪心算法题目的代码. 百度百科 ...

  5. #C++初学记录(动态规划 被3整除的子序列)

    原题:牛客网 动态规划dynamic programming 的入门级题目 题目描述 : 给你一个长度为50的数字串,问你有多少个子序列构成的数字可以被3整除 答案对1e9+7取模 输入描述: 输入一 ...

  6. #C++初学记录(动态规划(dynamic programming)例题1 钞票)

    浅入动态规划 dynamic programming is a method for solving a complex problem by breaking it down into a coll ...

  7. #C++初学记录(STL容器以及迭代器)

    STL初步 提交ACM会TLE /仅以学习STL与迭代器使用 C. Cards Sorting time limit per test1 second memory limit per test256 ...

  8. #C++初学记录(字符串与指针操作库函数)

    测试程序 #include<iostream> #include<cstring> using namespace std; int a[204],b[204],lena,n; ...

  9. Java实现 LeetCode 552 学生出勤记录 II(数学转换?还是动态规划?)

    552. 学生出勤记录 II 给定一个正整数 n,返回长度为 n 的所有可被视为可奖励的出勤记录的数量. 答案可能非常大,你只需返回结果mod 109 + 7的值. 学生出勤记录是只包含以下三个字符的 ...

随机推荐

  1. Python学习文档指引

    Python文档资源: 形式 角色 #注释 文件中的文档 dir函数 对象中可用属性的列表 文档字符串:__doc__ 附加在对象上的文件中的文档 PyDoc:help函数 对象的交互帮助 PyDoc ...

  2. Step by Step to create orders by consuming SAP Commerce Cloud Restful API

    Recently Jerry is working on an integration project about creating orders in Wechat platform by cons ...

  3. live555的使用(转载)

    Live555 是一个为流媒体提供解决方案的跨平台的C++开源项目,它实现了对标准流媒体传输协议如RTP/RTCP.RTSP.SIP等的支持.Live555实现 了对多种音视频编码格式的音视频数据的流 ...

  4. HDFS读流程

    客户端先与NameNode通信,获取block位置信息,之后线性地先取第一个块,然后接二连三地获取,取回一个块时会进行MD5验证,验证通过后会使read顺利进行完,当最终读完所有的block块之后,拼 ...

  5. 为群晖加把锁:使用ssh密钥保障数据安全

    对每一个使用群晖nas的人而言,能保证群晖里保存的数据不被未经授权的人访问下载甚至破坏可能是最重要的事情.但数据只要上网,就免不了担心密码被破解,群晖被侵入.现在网络上,要破解密码可能是最简单不过的事 ...

  6. 使用ImageMagick在Linux系统上截图

    ImageMagick安装指令: sudo apt-get install imagemagick 安装完成后,输入 import screenshot.png 命令就可以开始截图.此时鼠标图标会变成 ...

  7. systemctl 常用操作

    以samba为列 systemctl start smb       #启动smb服务 systemctl restart smb   #重启smb服务 systemctl stop smb      ...

  8. #2590. 「NOIP2009」最优贸易

    C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市.任意两个城市之间最多只有一条道路直接相连.这 m 条道路中有一部分为单向通行的道路,一部分为双向通行的道路,双向通行的道 ...

  9. Jquery的入门(一)

    Jquery介绍 Jquery是一个快速,简洁的JavaScript框架,是继Prototype之后又一个优秀的JavaScript代码库(或者是JavaScirpt框架),Jquery设计的宗旨是: ...

  10. 《The One!团队》第八次作业:ALPHA冲刺(四)

    项目 内容 作业所属课程 所属课程 作业要求 作业要求 团队名称 < The One !> 作业学习目标 (1)掌握软件测试基础技术.(2)学习迭代式增量软件开发过程(Scrum) 第四天 ...