【CodeForces - 1051A】Vasya And Password (构造,水题)
題干:
Vasya came up with a password to register for?EatForces?— a string?ss. The password in?EatForces?should be a string, consisting of lowercase and uppercase Latin letters and digits.
But since?EatForces?takes care of the security of its users, user passwords must contain at least one digit, at least one uppercase Latin letter and at least one lowercase Latin letter. For example, the passwords "abaCABA12", "Z7q" and "3R24m" are valid, and the passwords "qwerty", "qwerty12345" and "Password" are not.
A substring of string?ss?is a string?x=slsl+1…sl+len?1(1≤l≤|s|,0≤len≤|s|?l+1)x=slsl+1…sl+len?1(1≤l≤|s|,0≤len≤|s|?l+1).?lenlen?is the length of the substring. Note that the empty string is also considered a substring of?ss, it has the length?00.
Vasya's password, however, may come too weak for the security settings of?EatForces. He likes his password, so he wants to replace some its substring with another string of the same length in order to satisfy the above conditions. This operation should be performed?exactly?once, and?the chosen string should have the minimal possible length.
Note that the length of?ss?should not change after the replacement of the substring, and the string itself should contain only lowercase and uppercase Latin letters and digits.
Input
The first line contains a single integer?TT?(1≤T≤1001≤T≤100) — the number of testcases.
Each of the next?TT?lines contains the initial password?s?(3≤|s|≤100)s?(3≤|s|≤100), consisting of lowercase and uppercase Latin letters and digits.
Only?T=1T=1?is allowed for hacks.
Output
For each testcase print a renewed password, which corresponds to given conditions.
The length of the replaced substring is calculated as following: write down all the changed positions. If there are none, then the length is?00. Otherwise the length is the difference between the first and the last changed position plus one. For example, the length of the changed substring between the passwords "abcdef"?→→"a7cdEf" is?44, because the changed positions are?22?and?55, thus?(5?2)+1=4(5?2)+1=4.
It is guaranteed that such a password always exists.
If there are several suitable passwords — output any of them.
Example
Input
2 abcDCE htQw27Output
abcD4E htQw27Note
In the first example Vasya's password lacks a digit, he replaces substring "C" with "4" and gets password "abcD4E". That means, he changed the substring of length 1.
In the second example Vasya's password is ok from the beginning, and nothing has to be changed. That is the same as replacing the empty substring with another empty substring (length 0).
解題報告:
?
錯誤代碼1:
#include<bits/stdc++.h> #define ll long long using namespace std; string s; int main() {int t;cin>>t;while(t--) {cin>>s;int low=-1,upp=-1,dig=-1;int l=0,u=0,d=0;for(int i = 0; i<s.length(); i++) {if(s[i] >= 'a' && s[i] <= 'z') low = i,l++;if(s[i] >= 'A' && s[i] <= 'Z') upp = i,u++;if(s[i] >= '1' && s[i] <= '9') dig = i,d++; }if(low == -1) {if(u>=2) s[upp] = 'a';else s[dig] = 'a';}if(upp == -1) {if(l>=2) s[low] = 'A';else s[dig] = 'A';}if(dig == -1) {if(l>=2) s[low] = '1';else s[upp] = '1';}cout << s << endl;}return 0; }1wa在了思路上(代碼如上),這樣寫是不對的,因為有可能upp和dig都等于-1,所以都需要low進行更新,舉個例子“aaa”,就會發現樣例結果是錯的。
2wa在了判斷數字,,,應該是s[i]>='0' 為甚要寫>=‘1’? ? 怎么想的???
AC代碼:
#include<bits/stdc++.h> #define ll long long using namespace std; char s[505]; int l[505],u[505],d[505]; int cnt1,cnt2,cnt3; int main() {int t;cin>>t;getchar();while(t--) {gets(s);int len = strlen(s);int low=-1,upp=-1,dig=-1;cnt1=cnt2=cnt3=0;for(int i = 1; i<505; i++) l[i]=u[i]=d[i]=0;for(int i = 0; i<len; i++) {if(s[i] >= 'a' && s[i] <= 'z') low = i,l[++cnt1] = i;if(s[i] >= 'A' && s[i] <= 'Z') upp = i,u[++cnt2] = i;if(s[i] >= '0' && s[i] <= '9') dig = i,d[++cnt3] = i; }if(low == -1) {if(cnt2 >= 2) s[u[cnt2--]]='a';else s[d[cnt3--]] = 'a';}if(upp == -1) {if(cnt1>=2) s[l[cnt1--]] = 'A';else s[d[cnt3--]] = 'A';}if(dig == -1) {if(cnt1>=2) s[l[cnt1--]] = '1';else s[u[cnt2--]] = '1';}cout << s << endl;}return 0; }發現了別人的優秀代碼:嗯還是有很多可以學習的。。要善于運用vector等容器啊
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace std; using namespace __gnu_pbds; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vll = vector<ll>; using vpii = vector<pii>; using vpll = vector<pll>; template <typename T> using ost = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; #define forn(i,n) for(int i=0; i<int(n); ++i) #define all(x) (x).begin(), (x).end() #define pb push_back #define ff first #define ss secondint main() {ios::sync_with_stdio(false);int T;cin >> T;while (T--) {string s;cin >> s;bool lo=0, hi=0, dig=0;vi x;int n=s.size();for (int i=0; i<n; i++) {if (isdigit(s[i])) {if (dig) x.pb(i);dig=1;}else if (islower(s[i])) {if (lo) x.pb(i);lo=1;}else {if (hi) x.pb(i);hi=1;}}if (!dig) {s[x.back()]='1';x.pop_back();}if (!lo) {s[x.back()]='a';x.pop_back();}if (!hi) {s[x.back()]='A';x.pop_back();}cout << s << '\n';} }?
總結
以上是生活随笔為你收集整理的【CodeForces - 1051A】Vasya And Password (构造,水题)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sdii.exe - sdii是什么进程
- 下一篇: 浦发信用卡查询进度方法 四种方法简单快捷