Fxx and string
Accepts: 213
Submissions: 1867
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 131072/65536 K (Java/Others)
问题描述
青年理论计算机科学家Fxx得到了一个只包含小写字母的字符串。 字符串的长度为n,下标从1开始,第i位的字母为si,现在Fxx想知道有多少三元组(i,j,k)满足下列条件 1、i,j,k三个数成等比数列 2、si='y',sj='r',sk='x' 3.i/j和k/j中必须有整数
输入描述
第一行一个整数T(1≤T≤100)表示数据组数。 接下来T行,每行一个仅包含小写字母的字符串(字符串长度不超过10000)
输出描述
输出共T行。
每行一个整数表示答案。
输入样例
2 xyyrxx yyrrxxxxx
输出样例
0 2
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <map>
using namespace std;
const int maxn = 1e5;
char str[maxn];
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%s", str+1);
str[0] = ' ';
int ans = 0, len = strlen(str);
// cout << len << endl;
for(int i = 1; i < len; i++)
{
if(str[i] == 'y')
{
for(int j = i * 2; j < len; j += i)
{
if(str[j] == 'r')
{
int x = j * (j / i);
if(x < len)
{
if(str[x] == 'x')
ans++;
}
}
}
}
if(str[i] == 'x')
{
for(int j = i * 2; j < len; j += i)
{
if(str[j] == 'r')
{
int x = j * (j / i);
if(x < len)
{
if(str[x] == 'y')
ans++;
}
}
}
}
}
printf("%d\n", ans);
}
return 0;
}
Close