#C++初学记录(判断子串#数学结合)

时间:2022-12-19 08:01:48

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;
} }

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

#C++初学记录(判断子串#数学结合)

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

最后运行成功截图。

#C++初学记录(判断子串#数学结合)

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

  1. &num;C&plus;&plus;初学记录(素数判断2)

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

  2. &num;C&plus;&plus;初学记录(素数判断)

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

  3. &num;C&plus;&plus;初学记录(算法4)

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

  4. &num;C&plus;&plus;初学记录(sort函数)

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

  5. &num;C&plus;&plus;初学记录(动态规划 被3整除的子序列)

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

  6. &num;C&plus;&plus;初学记录(动态规划(dynamic programming)例题1 钞票)

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

  7. &num;C&plus;&plus;初学记录(STL容器以及迭代器)

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

  8. &num;C&plus;&plus;初学记录(字符串与指针操作库函数)

    测试程序 #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. Android库Volley的使用介绍

    Android Volley 是Google开发的一个网络lib,可以让你更加简单并且快速的访问网络数据.Volley库的网络请求都是异步的,你不必担心异步处理问题. Volley的优点: 请求队列和 ...

  2. lintcode:整数排序&vert;&vert;

    题目 给一组整数,按照升序排序.使用归并排序,快速排序,堆排序或者任何其他 O(n log n) 的排序算法. 解题 归并排序 public class Solution { /** * @param ...

  3. Entity Framework快速入门--IQueryable与IEnumberable的区别&lpar;转载&rpar;

    IEnumerable接口 公开枚举器,该枚举器支持在指定类型的集合上进行简单迭代.也就是说:实现了此接口的object,就可以直接使用foreach遍历此object: IQueryable 接口 ...

  4. Ubuntu 安装wireshark

    参考:ubuntu下安装wireshark 依赖及相关包的安装 1.编译工具 apt-get install build-essential 2.GTK+的开发文件和GLib库(libraries) ...

  5. Spark修炼之道(进阶篇)——Spark入门到精通:第九节 Spark SQL执行流程解析

    1.总体执行流程 使用下列代码对SparkSQL流程进行分析.让大家明确LogicalPlan的几种状态,理解SparkSQL总体执行流程 // sc is an existing SparkCont ...

  6. workflow--相关笔记

    转自http://blog.csdn.net/u014682573/article/details/29922093 1. 工作流技术 工作流(Workflow) 定义:工作流就是将一组任务组织起来, ...

  7. 简单易懂的程序语言入门小册子(5):基于文本替换的解释器,递归,不动点,fix表达式,letrec表达式

    这个系列有个显著的特点,那就是标题越来越长.忽然发现今天是读书节,读书节多读书. ==下面是没有意义的一段话============================================== ...

  8. poj 3258&quot&semi;River Hopscotch&quot&semi;&lpar;二分搜索&plus;最大化最小值问题&rpar;

    传送门 https://www.cnblogs.com/violet-acmer/p/9793209.html 题意: 有 N 块岩石,从中去掉任意 M 块后,求相邻两块岩石最小距离最大是多少? 题解 ...

  9. 高级数据类型--字典(dict)

    一.字典介绍 dict(字典) 是 除列表以外 Python 之中 最灵活 的数据类型.字典同样可以用来 存储多个数据,通常用于存储 描述一个 物体 的相关信息 和列表的区别: 列表 是 有序 的对象 ...

  10. PYTHON-网络通信 TCP

    网络编程: 学习网络编程 为什么?目的: 服务端特点: 网络通讯(通信) 什么是网络通讯? 为什么?目的:网络建立的目的是为数据交互(通信) 如何实现通讯(通信)? 互联网协议 互联网=物理连接介质+ ...