垂死挣扎-4
現在有一個字符串,你要對這個字符串進行 n 次操作,每次操作給出兩個數字:(p, l) 表示當前字符串中從下標為 p 的字符開始的長度為 l 的一個子串。你要將這個子串左右翻轉后插在這個子串原來位置的正后方,求最后得到的字符串是什么。字符串的下標是從 0 開始的,你可以從樣例中得到更多信息。
?
輸入描述:
每組測試用例僅包含一組數據,每組數據第一行為原字符串,長度不超過 10 ,僅包含大小寫字符與數字。接下來會有一個數字 n 表示有 n 個操作,再接下來有 n 行,每行兩個整數,表示每次操作的(p , l)。
保證輸入的操作一定合法,最后得到的字符串長度不超過 1000。
輸出描述:
輸出一個字符串代表最后得到的字符串。
?
輸入例子:
ab 2 0 2 1 3?
輸出例子:
abbaabb#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, const char * argv[]) {
string str;
while (cin>>str) {
int n;
cin >> n;
while (n --) {
int beg,len,index;
cin>>beg>>len;
string temp = str.substr(beg,len);
index = beg + len;
reverse(temp.begin(), temp.end());
str.insert(index, temp);
}
cout<<str<<endl;
}
return 0;
}
?
?
給定 x, k ,求滿足 x + y = x | y 的第 k 小的正整數 y 。 | 是二進制的或(or)運算,例如 3 | 5 = 7。
比如當 x=5,k=1時返回 2,因為5+1=6 不等于 5|1=5,而 5+2=7 等于 5 | 2 = 7。
?
輸入描述:
每組測試用例僅包含一組數據,每組數據為兩個正整數 x , k。 滿足 0 < x , k ≤ 2,000,000,000。
輸出描述:
輸出一個數y。
?
輸入例子:
5 1?
輸出例子:
2先上代碼:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <iostream> #include <bitset> using namespace std; int main() { ????unsigned?long long x, y = 1, k; ????cin >> x >> k; ????? ????std::bitset<64> xbs(x), kbs(k); ????for (size_t i = 0, kpos = 0; i < xbs.size(); ++i) { ????????if (! xbs.test(i)) {?// xbs[i] == 0 ????????????xbs.set(i, kbs[kpos++]); ????????} ????} ????y = xbs.to_ullong(); ????y ^= x; ????cout << y << endl;??? ????? ????return 0; } |
轉載于:https://www.cnblogs.com/Hanzo/p/5879407.html
總結
- 上一篇: 不变(Invariant), 协变(Co
- 下一篇: 屏蔽Xcode 8“烦人”的日志输出