日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

*【CodeForces - 122D】Lucky Transformation(字符串问题,思维剪枝,优化,有坑,需注意的问题if的层次总结)

發布時間:2023/12/10 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 *【CodeForces - 122D】Lucky Transformation(字符串问题,思维剪枝,优化,有坑,需注意的问题if的层次总结) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits?4?and?7. For example, numbers?47,?744,?4?are lucky and?5,?17,?467?are not.

Petya has a number consisting of?n?digits without leading zeroes. He represented it as an array of digits without leading zeroes. Let's call it?d. The numeration starts with?1, starting from the most significant digit. Petya wants to perform the following?operation?k?times: find the minimum?x?(1?≤?x?<?n)?such that?dx?=?4?and?dx?+?1?=?7, if?x?is odd, then to assign?dx?=?dx?+?1?=?4, otherwise to assign?dx?=?dx?+?1?=?7. Note that if no?x?was found, then the operation counts as completed and the array doesn't change at all.

You are given the initial number as an array of digits and the number?k. Help Petya find the result of completing?k?operations.

Input

The first line contains two integers?n?and?k?(1?≤?n?≤?105,?0?≤?k?≤?109)?— the number of digits in the number and the number of completed operations. The second line contains?n?digits without spaces representing the array of digits?d, starting with?d1. It is guaranteed that the first digit of the number does not equal zero.

Output

In the single line print the result without spaces — the number after the?koperations are fulfilled.

Examples

Input

7 4 4727447

Output

4427477

Input

4 2 4478

Output

4478

Note

In the first sample the number changes in the following sequence:?4727447?→?4427447?→?4427477?→?4427447?→?4427477.

In the second sample:?4478?→?4778?→?4478.

題目大意:

? ? 給定一個字符串,每次從左到右找第一個 47 ,然后判斷其起始下標是偶數還是奇數,若為奇數,則替換為 44 ,否則替換為 77 ,最多操作 k 次,求最終結果。

解題報告:

? 這題需要注意的是數據范圍。 顯然我們把 47 替換為 44 只會對當前位置以后產生影響,而替換為 77 則會對當前位置以前產生影響。若串中 i?1 i-1 的位置也為 4 ,則會發現 447 這部分會產生循環,此時可以直接得出結果,否則遍歷一下原串照題意修改即可。

? 值得注意的是,剛開始寫的時候TLE了,是循環了多次,后來發現是不需要這樣的,直接遍歷一遍數組順便更改字符串就好,主要還是因為那句:只有替換為77,才可能對以前的位置產生影響,否則的話都是直接遍歷一遍就好了,不需要每次都從頭找。

? 還有一個要注意的地方,我是用string讀的字符串,所以奇偶正好和題干敘述相反才對。

AC代碼:

#include<string> #include<iostream> #define ll long long using namespace std; const int MAX = 1e5 +5; int k,n; int main() {string s;cin>>n >> k;cin>>s;int len = s.length();int flag = 0;for(int i = 0; i<len-1 && k; i++) {if(s[i] == '4' && s[i+1] == '7') {if(i>=1 && s[i-1] == '4') {if(i%2==1) {if(k%2==1) {s[i] = '7';}k-=k,flag=1;continue;}}if(i%2==0) s[i+1]='4';else s[i]='7';flag=1;k--;}}cout << s << endl;return 0 ;}

1TLE:原因在于程序就寫的和初心不符。那句本來就該放到這個if外面的。

#include<string> #include<iostream> #define ll long long using namespace std; const int MAX = 1e5 +5; int k,n; int main() {string s;cin>>n >> k;cin>>s;int len = s.length();while(k>0) {int flag = 0;for(int i = 0; i<len-1; i++) {if(s[i] == '4' && s[i+1] == '7') {if(i>=1 && s[i-1] == '4') {if(i%2==1) {if(k%2==1) {s[i] = '7',k-=k,flag=1;break;//k-=k,flag=1;break;這三句應該放到這個if的外面}}}if(i%2==0) s[i+1]='4';else s[i]='7'; flag=1;k--;break;}}if(flag == 0) break;}cout << s << endl;return 0 ;}

2TLE:這次就真的是因為思路的原因TLE了,考慮到了? ?掃到447時可以直接出結果,但是還是有數據可以卡到O(nk)的復雜度,所以肯定還是說不過去的。

#include<string> #include<iostream> #define ll long long using namespace std; const int MAX = 1e5 +5; int k,n; int main() {string s;cin>>n >> k;cin>>s;int len = s.length();while(k>0) {int flag = 0;for(int i = 0; i<len-1; i++) {if(s[i] == '4' && s[i+1] == '7') {if(i>=1 && s[i-1] == '4') {if(i%2==1) {if(k%2==1) {s[i] = '7';}k-=k,flag=1;break;}}if(i%2==0) s[i+1]='4';else s[i]='7'; flag=1;k--;break;}}if(flag == 0) break;}cout << s << endl;return 0 ;}

3WA:(由TLE到WA、、、Orz)其實這次原因也很簡單,就是直接在上面那個程序上改的,所以break直接去掉了,改成ifelse了。。其實不應該這樣的,應該continue才對。因為對于這個代碼3,如果一個樣例是4447,循環到44‘4’7的時候,進入了1,但是沒進入2,本意是跳到3執行,但是這樣的話就沒執行3,而是直接i++到下一個for中了。

#include<string> #include<iostream> #define ll long long using namespace std; const int MAX = 1e5 +5; int k,n; int main() {string s;cin>>n >> k;cin>>s;int len = s.length(); // while(k>0) {int flag = 0;for(int i = 0; i<len-1 && k; i++) {if(s[i] == '4' && s[i+1] == '7') {if(i>=1 && s[i-1] == '4') { //1if(i%2==1) { //2if(k%2==1) {s[i] = '7';}k-=k,flag=1;}}else { //3if(i%2==0) s[i+1]='4';else s[i]='7';flag=1;k--;}}} // }cout << s << endl;return 0 ;}

總結:? 還是要注意if的層次啊!是并列?還是包含關系?再看一遍這題!

總結

以上是生活随笔為你收集整理的*【CodeForces - 122D】Lucky Transformation(字符串问题,思维剪枝,优化,有坑,需注意的问题if的层次总结)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。