时间限制: 1 Sec 内存限制: 128 MB
提交: 113 解决: 45
[提交] [状态] [讨论版] [命题人:admin]
题目描述
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.
输入
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.
输出
For each test case, output one line containing “y” where y is the number of target substrings.
样例输入
3
qwertyuiop
qqwweerrttyyuuiioopp
aaaaaaaaaa
样例输出
10
30
55
提示
1≤T≤20,1≤len(String)≤10^5,1≤∑len(string)≤10^6
Strings only contain lowercase English letters.
照题意,简单得出ans = (1+cnt)*cnt/2(cnt为每个连续的相同字母的个数)
然后5min左右写完程序了,一交,wa,思索半天,觉得很对,改了改其他小地方,wa
1h后,突然顿悟ll ans = a*b(a,b为整型),爆炸,
为啥我记得是a*b会以长整型运算然后再变成long long啊qwq
这是wa的代码
#include
using namespace std;
typedef long long ll;
char a[1000006];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(a,0,sizeof(a));
scanf("%s",a);
ll ans = 0;
for(int i=0;a[i];i++)
{
///printf("%c",a[i]);
int cnt = 1;
while(a[i+1]&&a[i]==a[i+1])cnt++,i++;
ans += (1+cnt)*cnt/2;
}
printf("%lld\n",ans);
}
return 0;
}
这是AC的代码
#include
using namespace std;
typedef long long ll;
char a[1000006];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(a,0,sizeof(a));
scanf("%s",a);
ll ans = 0;
for(int i=0;a[i];i++)
{
///printf("%c",a[i]);
ll cnt = 1;
while(a[i+1]&&a[i]==a[i+1])cnt++,i++;
ans += (1+cnt)*cnt/2;
}
printf("%lld\n",ans);
}
return 0;
}