【ZOJ-1633】解题报告(字符串递推,二分查找)
原始题目
Big String
- Time Limit: 2 Seconds
- Memory Limit: 65536 KB
We will construct an infinitely long string from two short strings: A = "__" (four characters), and B = "T.T" (three characters). Repeat the following steps: - Concatenate A after B to obtain a new string C. For example, if A = "__" and B = "T.T", then C = BA = "T.T__". - Let A = B, B = C -- as the example above A = "T.T", B = "T.T__".
Your task is to find out the n-th character of this infinite string. ### Input
The input contains multiple test cases, each contains only one integer \(N (1 \le N \le 2^{63} - 1)\). Proceed to the end of file.
Output
For each test case, print one character on each line, which is the N-th (index begins with 1) character of this infinite string.
Sample Input
1
2
4
8
###Sample Output
T
.
^
T
Author:
CHENG, Long ### Source: Zhejiang University Local Contest 2003
题目大意
由A="__" , B="T.T" , C=BA="T.T__" ,再令C=B, B=A,重复这种操作,问如此构成的字符串第n个字符是什么。
解题思路
- \(dp[i]\) 表示第i个字符串由多少个字符构成,已知\(dp[0]=4,dp[1]=3,dp[2]=7\),根据递推规则可知\(dp[i+1]=dp[i]+dp[i-1]\)
- 折半查找第一个比n大的dp[i],则第i串里的第n位与第i-1串里的第n-dp[i-1](可以保证这一项大于零)位对应字符相同。递归操作直至n<7。从"T.T__"寻找就可以了
解题代码
1 |
|
收获与反思
- 二分查找与递推的结合
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!