HDU 5944 Fxx and string

Fxx and string

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 567    Accepted Submission(s): 236


Problem Description
Young theoretical computer scientist Fxx get a string which contains lowercase letters only.

The string  S  contains  n  lowercase letters  S1S2Sn .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、 Si= ' y ' ,Sj= ' r ' ,Sk= ' x '.

3.Either j|i or j|k
 

Input
In the first line, there is an integer  T(1T100)  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 ).
 

Output
For each case, output the answer.
 

Sample Input
  
  
2 xyyrxx yyrrxxxxx
 

Sample Output
  
  
0 2
 

Source
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   5960  5959  5958  5957  5956 

正反判断一下就行,在网上看的他们枚举的公比的,感觉挺厉害,小编这个大水货就没想起来。恩,付下那哥们的代码
http://blog.csdn.net/qq_32944513/article/details/52968100
  1. #include <iostream>  
  2. #include<stdio.h>  
  3. #include<string.h>  
  4. using namespace std;  
  5. char s[10005];//si=s_i=s​i​​='yyy',sj=,s_j=,s​j​​='rrr',sk=,s_k=,s​k​​='xxx'  
  6. int main()  
  7. {  
  8.     int T,i,j,k,q,sum;  
  9.     cin>>T;  
  10.     while(T--)  
  11.     {  
  12.         scanf("%s",s);  
  13.         int l=strlen(s);  
  14.         sum=0;  
  15.         for(int r=0;r<l;r++)  
  16.         {  
  17.             i=r+1;  
  18.             for(q=2;;q++)//枚举公比  
  19.             {  
  20.                 j=i*q;  
  21.                 k=j*q;  
  22.                 if(k>l) break;  
  23.                 if(s[i-1]=='y'&&s[j-1]=='r'&&s[k-1]=='x')  
  24.                     sum++;  
  25.                 if(s[i-1]=='x'&&s[j-1]=='r'&&s[k-1]=='y')//小于1的公比  
  26.                     sum++;  
  27.             }  
  28.         }  
  29.         cout<<sum<<endl;  
  30.     }  
  31.     return 0;  
  32. }  


下面就是我的代码,也可以ac的,感觉更容易想到
#include <stdio.h>
#include <string.h>
#include <math.h>

int main()
{
	int t,num,i,j,k,cnt;
	char a[10005],b[10005];
	
	scanf("%d",&t);
	getchar();
	while(t--)
	{
		gets(a);
		num=strlen(a);
		cnt=0;
		for(i=1;i<=num;i++)
		{
			if(a[i-1]=='y')
			{
				for(j=2;i*j<=num;j++)
				{
					if(a[i*j-1]=='r')
						if(i*j*j-1 <= num && a[i*j*j-1] == 'x')
							cnt++;
				}
			}
		}
		for(i=1;i<=num;i++)
		{
			if(a[i-1]=='x')
			{
				for(j=2;i*j<=num;j++)
				{
					if(a[i*j-1]=='r')
						if(i*j*j-1 <= num && a[i*j*j-1] == 'y')
							cnt++;
				}
			}
		}
		
		printf("%d\n",cnt);
	}
	
	return 0;
}


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值