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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

CodeForces - 1213E Two Small Strings(暴力+构造)

發布時間:2024/4/11 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CodeForces - 1213E Two Small Strings(暴力+构造) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:點擊查看

題目大意:給出一個字符串s和字符串t,s和t的長度皆為2,現在要求我們構造出一個字符串res,他的長度是3*n,要求字母a和字母b還有字母c各出現3次,并滿足字符串s和字符串t不能是字符串res的子字符串,構造出任意一種即可

題目分析:一開始看到這個題目感覺直接莽就行了,答案肯定都是YES,所以對于字符串“abc”,將其全排列分別擴大至n倍然后判斷一下就行了,但是在test10WA了一發,其實只是這樣思考會漏情況的,從cf拿到hack數據后就明白該怎么做了,若給出一組數據ab和ac,關于abc的全排列,若n大于等于2的話,首尾相接,那么a后面必定不是b就是c,這樣就無法構造出滿足條件的數據了,為了應對這種情況,我們可以增加每個字母連在一起的情況,比如aaabbbccc,這樣就能完美應對ab,ac這種數據了,而其余的數據用全排列構造出來的答案也足以滿足條件

面向數據編程。。

其實在WA了第一發之后,可以自己寫一個程序把所有能hack掉剛才全排列擴大至n倍這個思想的所有反例都打出來,這樣面向數據編程也不是不可以,下面再掛一下輸出hack樣例的程序

不得不說基于范圍的for真好用,懶人專屬(比如我)

代碼:

#include<iostream> #include<cstdlib> #include<string> #include<cstring> #include<cstdio> #include<algorithm> #include<climits> #include<cmath> #include<cctype> #include<stack> #include<queue> #include<list> #include<vector> #include<set> #include<map> #include<sstream> #include<unordered_map> using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=2e5+100;int main() { // freopen("input.txt","r",stdin); // ios::sync_with_stdio(false);int n;string s,t;cin>>n>>s>>t;string abc="abc";vector<string>ans;do{string temp;for(int i=1;i<=n;i++)temp+=abc;ans.push_back(temp);//全排列ans.push_back(string(n,abc[0])+string(n,abc[1])+string(n,abc[2]));//相鄰字母相同情況}while(next_permutation(abc.begin(),abc.end()));for(auto ss:ans){if(ss.find(s)==string::npos&&ss.find(t)==string::npos){cout<<"YES"<<endl<<ss<<endl;break;}}return 0; }

構造hack掉全排列擴大的程序:

#include<iostream> #include<cstdlib> #include<string> #include<cstring> #include<cstdio> #include<algorithm> #include<climits> #include<cmath> #include<cctype> #include<stack> #include<queue> #include<list> #include<vector> #include<set> #include<map> #include<sstream> #include<unordered_map> using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=2e5+100;vector<string>ans2;vector<string>ans;void check() {for(auto i:ans2)for(auto j:ans2){int cnt=0;for(auto s:ans){if(s.find(i)!=string::npos||s.find(j)!=string::npos)cnt++;if(cnt==ans.size()){cout<<i<<' '<<j<<endl;}}} }int main() { // freopen("input.txt","r",stdin); // ios::sync_with_stdio(false);int n=3;string abc="abc";do{string temp;for(int i=1;i<=n;i++)temp+=abc;ans.push_back(temp);ans2.push_back(abc.substr(0,2)); // cout<<temp<<endl;}while(next_permutation(abc.begin(),abc.end()));ans2.push_back("aa");ans2.push_back("bb");ans2.push_back("cc");check();return 0; }

?

總結

以上是生活随笔為你收集整理的CodeForces - 1213E Two Small Strings(暴力+构造)的全部內容,希望文章能夠幫你解決所遇到的問題。

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