魔法师的咏唱
来源:互联网 发布:三国志10 兵种 数据 编辑:程序博客网 时间:2024/05/01 19:54
Problem Statement
Magical Girls are girls who have magical powers. They fight against evil witches to protect the Earth.
You, one of the Magical Girls, are now fighting against an evil witch. You want to use ultimate magic to defeat the witch. To use ultimate magic you have to chant a magical spell. The spell is determined with a sequence A.
A is the sequence of strings which is determined with a sequence first. Each element of first is a string which contains only '0' and '1'. Let K be the number of elements of first. Each element of A is defined as follows:
for all i, 0 <= i < K, A[i] = first[i].
Otherwise, A[i] = A[i-1] + A[i-K-1] + A[i-2K-1] + ... (while i-m*K-1 is not less than 0). Here operator '+' denotes concatenation of strings.
You are given three integers n, lo and hi. Let S be the substring of A[n] from lo to hi, inclusive. That is, S = A[n][lo..hi] where both lo and hi are 0-based indices into A[n]. The spell you have to chant is S and the power of the magic is equal to the length of the longest contiguous substring of S which contains only '1' characters. If S contains only '0' characters, then the power of the magic is equal to 0. Return the power of the ultimate magic.
Definition
Class:
MagicalGirlLevelThreeDivOne
Method:
theMaxPower
Parameters:
vector <string>, int, long long, long long
Returns:
long long
Method signature:
long long theMaxPower(vector <string> first, int n, long long lo, long long hi)
(be sure your method is public)
Constraints
-
first will contain between 1 and 50 elements, inclusive.
-
Each element of first will contain between 1 and 50 characters, inclusive.
-
Each element of first will consist of '0' (zero) and '1' (one).
-
n will be between 0 and 1,000,000,000, inclusive.
-
hi will be between 0 and min{1,000,000,000,000,000 (10^15), (length of A[n])-1}, inclusive.
-
lo will be between 0 and hi, inclusive.
Examples
0)
{"111", "011"}
4
2
7
Returns: 4
A[2] = "011", A[3] = "011111", A[4] = "011111011" S = A[4][2..7] = "111101"
1)
{"1", "11", "111"}
123456789
123456789
123456789012345
Returns: 123456665555557
2)
{"0", "00", "000"}
987654321
987654321
987654321054321
Returns: 0
3)
{"1110", "11", "11111", "111", "1"}
314159265
271828182845904
314159265358979
Returns: 15
4)
{"10000", "011011001011000001101000010100000111000110110",
"000001010001011000001101110", "100100000110100001010000",
"011011010", "01100000010101101110000011100011001000",
"0001010", "010011000", "000101001", "00", "1"}
1000000000
1000000000000000
1000000000000000
Magical Girls are girls who have magical powers. They fight against evil witches to protect the Earth.
You, one of the Magical Girls, are now fighting against an evil witch. You want to use ultimate magic to defeat the witch. To use ultimate magic you have to chant a magical spell. The spell is determined with a sequence A.
A is the sequence of strings which is determined with a sequence first. Each element of first is a string which contains only '0' and '1'. Let K be the number of elements of first. Each element of A is defined as follows:
for all i, 0 <= i < K, A[i] = first[i].
Otherwise, A[i] = A[i-1] + A[i-K-1] + A[i-2K-1] + ... (while i-m*K-1 is not less than 0). Here operator '+' denotes concatenation of strings.
You are given three integers n, lo and hi. Let S be the substring of A[n] from lo to hi, inclusive. That is, S = A[n][lo..hi] where both lo and hi are 0-based indices into A[n]. The spell you have to chant is S and the power of the magic is equal to the length of the longest contiguous substring of S which contains only '1' characters. If S contains only '0' characters, then the power of the magic is equal to 0. Return the power of the ultimate magic.
Definition
Class:
MagicalGirlLevelThreeDivOne
Method:
theMaxPower
Parameters:
vector <string>, int, long long, long long
Returns:
long long
Method signature:
long long theMaxPower(vector <string> first, int n, long long lo, long long hi)
(be sure your method is public)
Constraints
-
first will contain between 1 and 50 elements, inclusive.
-
Each element of first will contain between 1 and 50 characters, inclusive.
-
Each element of first will consist of '0' (zero) and '1' (one).
-
n will be between 0 and 1,000,000,000, inclusive.
-
hi will be between 0 and min{1,000,000,000,000,000 (10^15), (length of A[n])-1}, inclusive.
-
lo will be between 0 and hi, inclusive.
Examples
0)
{"111", "011"}
4
2
7
Returns: 4
A[2] = "011", A[3] = "011111", A[4] = "011111011" S = A[4][2..7] = "111101"
1)
{"1", "11", "111"}
123456789
123456789
123456789012345
Returns: 123456665555557
2)
{"0", "00", "000"}
987654321
987654321
987654321054321
Returns: 0
3)
{"1110", "11", "11111", "111", "1"}
314159265
271828182845904
314159265358979
Returns: 15
4)
{"10000", "011011001011000001101000010100000111000110110",
"000001010001011000001101110", "100100000110100001010000",
"011011010", "01100000010101101110000011100011001000",
"0001010", "010011000", "000101001", "00", "1"}
1000000000
1000000000000000
1000000000000000
Returns: 1
傻瓜算法
#include <vector>#include <string>using namespace std;class MagicalGirlLevelThreeDivOne{public:long long theMaxPower( vector<string> first, int n, long long lo, long long hi ){// calculate Anstring An;vector<string> A = first;int K = first.size();if( n >= K ) A.push_back( first[K-1] );if( n > K ) {for( int i = K + 1; i < 2*K; i++ ){A.push_back( A[i-1] + A[i-K-1] );}}if( n < 2*K ) An = A[n];else{vector<string> buff( A.begin() + K, A.begin() + 2*K );string newA;for( int i = 2*K; i <= n; i++ ){newA = buff[K-1] + buff[0];buff.erase( buff.begin() );buff.push_back( newA );}An = newA;}long long i = lo, numOfOne = 0, maxOnes = 0;while( i < hi && i < An.size() ){if( An[i] == '1'){numOfOne++;if( numOfOne > maxOnes ) maxOnes = numOfOne;}else{numOfOne = 0;}i++;}return maxOnes;}};
- 魔法师的咏唱
- 魔法师
- 魔法师
- 程序员-IT领域的魔法师
- 魔法师突然出现的C++
- 如何成为一个强大的IT魔法师?
- Android 变脸谁是幕后的魔法师?
- 输入的魔法师,cherry机械键盘介绍
- Android变脸幕后的魔法师:各巨头…
- 科大讯飞胡郁演讲实录:人工智能是这个时代最伟大的魔法师
- 1644: 魔法师
- 【tyvj1863】【codevs1995】黑魔法师之门,有毒的并查集
- 神奇的魔法师——教你在手机上如何一键创建相册
- 神奇的魔法师——教你在手机上如何一键创建相册
- ifrog 1129 喵哈哈村的战斗魔法师丶坏坏い月 线段树||分块
- 玲珑杯1129-喵哈哈村的战斗魔法师丶坏坏い月
- 玲珑学院OJ 1129 喵哈哈村的战斗魔法师丶坏坏い月【暴力分块】
- 程序员和魔法师
- Linux下基于erlang的内存监控
- 关于Linux文件模式16位材料的收集
- POJ3268—Silver Cow Party
- 开启测试新起航
- eclipse windows 窗口背景颜色 保护视力
- 魔法师的咏唱
- 标签
- 实现ssh自动登录的4种方法
- Linux下NFS的建立与配置方法
- zoj_2966 Build The Electric System
- 长安出行难:一口锅做两样饭 长安公交大吐苦水
- Unity 3D 现在开发移动应用免费了
- css文件编码问题
- Linux 下C使用XML传递消息(字符串)