bestcoder
来源:互联网 发布:精准营销软件 编辑:程序博客网 时间:2024/05/02 12:11
题意为有n个字符串,每个字符串仅由小写字母组成,求每个字符串中存在多少个三元组(i,j,k)使i,j,k成等比数列,并且s[i-1]='y',s[j-1]='r',s[k-1]='x'。
解题思路:
既然i,j,k成等比数列,则它们之间肯定存在整数倍关系,设公比为2,判断第二个条件是否成立,再设公比为3,判断是否成立,依次类推。。。若字符数组下标越界,若越界, 则退出循环。
参考代码:
#include<iostream>#include<cstring>using namespace std;int main(){ int n; cin>>n; while(n--) { char s[10005]; int i; cin>>s; int len=strlen(s); int cnt=0; for(i=0;i<len;i++) { if(s[i]=='y'){int i1=i+1;for(int j=2;;j++){int k1=i1*j;int j1=k1*j;if(k1>len||j1>len) break;if(s[k1-1]=='r'&&s[j1-1]=='x') cnt++;}} } cout<<cnt<<endl; } return 0;}
注意:解决本题时应考虑怎么设计算法才能使时间复杂度尽量小,否则将导致超时。
0 0
- BestCoder
- bestcoder
- BestCoder 20140727
- BestCoder#3
- Bestcoder #11
- bestcoder #14
- BestCoder #20
- bestcoder lines
- BestCoder#27
- BestCoder#29
- bestcoder Delete
- bestcoder Mutiple
- Bestcoder #40
- bestcoder #45
- Bestcoder Untitled
- Bestcoder #49
- BestCoder #50
- BestCoder #52
- keil4程序从JLINK8下载后,不能运行,需要重启的原因
- test code
- 1012. The Best Rank (25)
- js 对象浅拷贝和深拷贝
- PHP操作Memcached
- bestcoder
- uva 10596Morning Walk
- 习题2-6 排列 permutation
- 百度地图API解决 android 6.0定位不了的问题
- 一心多用多线程-线程安全问题
- UI设计师如何做性能优化?
- 使用线程完成简单的生产者和消费者
- 安装VMware以及centos的初步介绍
- poj3641 简单数论 快速幂 取模,素数