Fxx and string
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 715 Accepted Submission(s): 311
Problem Description
Young theoretical computer scientist Fxx get a string which contains lowercase letters only.
The string S
contains
n
lowercase letters
S
1
S
2
…S
n![]()
.Now Fxx wants to know how many three tuple
(i,j,k)
there are which can meet the following conditions:
1、 i,j,k
are adjacent into a geometric sequence.
2、 S
i
=
'
y
'
,S
j
=
'
r
'
,S
k
=
'
x
'.
3.Either j|i or j|k
The string S
1、 i,j,k
2、 S
3.Either j|i or j|k
Input
In the first line, there is an integer
T(1≤T≤100)
indicating the number of test cases.
T
lines follow, each line contains a string, which contains only lowercase letters.(The length of string will not exceed
10000
).
T
Output
For each case, output the answer.
Sample Input
2 xyyrxx yyrrxxxxx
Sample Output
0 2
Source
Recommend
题意:给定一个字符串,找出符合要求的:下标成等比数列且对应字母与题中相符。注意1 2 4与4 2 1是两种
题解:一开始想到的是三重循环,不用试也知道超时,所以可以循环比例数j,i为数列最小值,随后是i*j,i*j*j
code:
#include<cstdio>
#include<cstring>
char s[10000];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s",s+1);
int l=strlen(s+1);
int num=0;
for(int i=1;i<=l;i++)
{
for(int j=2;i*j*j<=l;j++)
{
if(s[i]=='y'&&s[i*j]=='r'&&s[i*j*j]=='x')
num++;
if(s[i]=='x'&&s[i*j]=='r'&&s[i*j*j]=='y')
num++;
}
}
printf("%d\n",num);
}
return 0;
}