#C++初学记录(判断子串#数学结合)
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++初学记录(判断子串#数学结合)的更多相关文章
- #C++初学记录(素数判断2)
素数判断2 比较简单的算法,没有技术含量 A prime number is a natural number which has exactly two distinct natural numbe ...
- #C++初学记录(素数判断)
练习题目二 素数判断 A prime number is a natural number which has exactly two distinct natural number divisors ...
- #C++初学记录(算法4)
A - Serval and Bus It is raining heavily. But this is the first day for Serval, who just became 3 ye ...
- #C++初学记录(sort函数)
sort函数 前言:当进行贪心算法的学习时,需要用到sort函数,因为初学c++汇编语言,sort的具体用法没有深入学习,所以这里进行sort学习记录并只有基础用法并借用贪心算法题目的代码. 百度百科 ...
- #C++初学记录(动态规划 被3整除的子序列)
原题:牛客网 动态规划dynamic programming 的入门级题目 题目描述 : 给你一个长度为50的数字串,问你有多少个子序列构成的数字可以被3整除 答案对1e9+7取模 输入描述: 输入一 ...
- #C++初学记录(动态规划(dynamic programming)例题1 钞票)
浅入动态规划 dynamic programming is a method for solving a complex problem by breaking it down into a coll ...
- #C++初学记录(STL容器以及迭代器)
STL初步 提交ACM会TLE /仅以学习STL与迭代器使用 C. Cards Sorting time limit per test1 second memory limit per test256 ...
- #C++初学记录(字符串与指针操作库函数)
测试程序 #include<iostream> #include<cstring> using namespace std; int a[204],b[204],lena,n; ...
- Java实现 LeetCode 552 学生出勤记录 II(数学转换?还是动态规划?)
552. 学生出勤记录 II 给定一个正整数 n,返回长度为 n 的所有可被视为可奖励的出勤记录的数量. 答案可能非常大,你只需返回结果mod 109 + 7的值. 学生出勤记录是只包含以下三个字符的 ...
随机推荐
- 记支付宝接口对接,涉及到提取证书SN号的解决方案
支付宝针对.NET SDK并未封装有提取证书SN序列号的方法,仅针对Java平台才有对应的方法(赤裸裸的歧视啊~~) 要想在提取这个SN序列号有两种方案: 1. 直接用Java SDK包来提取SN 2 ...
- UCOSIII软件定时器
API函数 //创建 void OSTmrCreate (OS_TMR *p_tmr, CPU_CHAR *p_name, OS_TICK dly, OS_TICK period, OS_OPT op ...
- Laravel5.6---搜索查询 自带paginate()分页 get传参
laravel的paginate()分页,如果用post传参,点击第二页时会默认使用get,就会返回原始数据了 需要把查询数据get方式也放到paginate()分页参数中 一.路由 Route::g ...
- C#验证邮箱,电话,手机,数字,英文,日期,身份证,邮编,网址,IP类等常用函数封装
#region 验证邮箱验证邮箱 /**//// <summary> /// 验证邮箱 /// </summary> /// <param name="sour ...
- Python学习日记(十六) time模块和random模块
time模块 python表示时间的三种方式:时间戳.元祖(struct_time).格式化时间字符串 三种格式之间的转换: 1.时间戳 就是从1970年1月1日0点0分0秒开始按秒计算的偏移量,时间 ...
- Centos7安装防火墙firewall
安装 1.下载 yum install -y firewalld yum install -y firewall-config 2.启动 systemctl start firewalld # 启动 ...
- workman 使用心得
1. 服务端调试: 直接在 Events.php 中 echo 变量, 即可在 命令行工具中 看到输出的信息. 以便进行调试. 2. 客户端调试: 由于是js代码, 可以直接 用 conso ...
- springboot2.1.3+Junit4 单元测试
引入依赖的包: <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core ...
- Bash基础——工作管理(Job control)
注:1.这里说的Bash不单纯的指Bash,泛指shell 2.这里的后台指的是Bash下面避免任务(Jobs)被Ctrl+C中断的一种场景,与我们说的deamon那种后台工作的进程不是一个概念,注意 ...
- redis问题解决 Caused by: io.lettuce.core.RedisException: io.lettuce.core.RedisConnectionException: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specifie
1找到redis的配置文件 redis.conf vim redis.conf 修改 protected-mode yes 改为 protected-mode no 注释掉 #bin 127.0.0 ...