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. 在safari下input的placeholder设置行高失效

    在项目中遇到input的placeholder在safari下设置行高失效的问题,亲测 input{ width:250px; height:30px; line-height:30px; font- ...

  2. LinuxKernel优秀博客

    1.vanbreaker的专栏 2.LinuxKernel Exploration 3.DroidPhone的专栏 4.Linux内核研究以及学习文档和ARM学习以及研究的开放文档   [力荐] 5. ...

  3. SpringBoot学习<一>——快速搭建SpringBoot

    这是我的第一篇博客,博客记录我以后的学习,包括一些总结之类的东西,当然,这些记录是针对于与我个人而言的,可能有些地方会有不好的,或者出现错误,欢迎大家来指正(如果有人看的话)废话不多说.进入正题:Sp ...

  4. mysql的左连接问题

    之前写过一个mysql语句,功能是将一个表ds的一个字段值同步更新到另一个表bk的字段,不过不是全部,只更新表bk中有的数据,如果表bk中有而表ds中没有,表B对应的这个字段值就为空 UPDATE b ...

  5. 用c#监控网络流量

    using System; using System.Text; using System.Net; using System.Net.Sockets; using System.Runtime.In ...

  6. Python学习日记(六) 浅深copy

    浅深copy即完全复制一份和部分复制一份 浅深copy在列表数据量较大时不建议使用,比较消耗内存资源 1.赋值运算 l1 = [1,'s',[1,2,3]] l2 = l1 print(id(l1), ...

  7. oracle命令行导出、导入dmp文件

    1.导出语句: exp test/test@127.0.0.1:1521/orcl file=d:\gpmgt.dmp full=n: 导出test用户数据库对象,full=n表示默认只导出test用 ...

  8. java--Annotation实现

    Annotation实现 在java中一共提供了三个annotation:@Override,@Deprecated,@SupperessWarnings 代码范例:使用Annotation pack ...

  9. Linux查找工具locate和find

    linux 中有很多查找工具,今天主要讲解locate,find两个工具. 一.locate 1.性能介绍 非实时查找(数据库查找):locate 查询系统上预建的文件索引数据库 /var/lib/m ...

  10. Java List<String> list=new ArrayList<String>();为什么要声明为List,而不是ArrayList<String>

    例如:代码List list = new ArrayList(); 下面通过list来操作集合.假如代码编写后却发现集合使用的不准确,应该使用LinkedList,那么只要修改一行代码List lis ...