ECJTU 2018 Summer Training
E. Accepted Passwords(Gym - 100989E)
Islam is usually in a hurry. He often types his passwords incorrectly. He hates retyping his password several times whenever he tries to login, especially that his passwords are usually very long. He believes that websites should be tolerant with very long passwords. In other words, he believes that if a password is very long, and there is only one mistake in the password, the website should allow the user to login.
Your task is to check if an entered password should be accepted according to Islam, or not. The entered password will be accepted if it matches the user’s password, or if the user’s password length is at least 8 characters and the user made a mistake with only one character (either replaced it with a wrong character or dropped it).
Given the user’s password, and the entered password, determine if the entered password should be accepted according to Islam.
The first line of input contains the user’s password.
The second line of input contains the entered password.
Both strings contain only lowercase and uppercase English letters.
The length of each string is at least 1 and at most 100.
Print yes if the entered password should be accepted according to Islam, otherwise print no.
AgentMahone
IslamIsMahone
no
ofmahone
ofmahome
yes
这道题先讨论字母长等长的情况,在讨论错的字母数。再讨论只大于一个字母的情况,再讨论错误的字母数。请他情况都是NO;
1 #include<iostream> 2 #include<algorithm> 3 #include<cmath> 4 #include<string> 5 #define max 100000+2 6 using namespace std; 7 int main() 8 { 9 string a,b; 10 while(cin>>a>>b) 11 { 12 int ans; 13 int l1=a.size(); 14 int l2=b.size(); 15 if(l1==l2) 16 { 17 ans=0; 18 for(int i=0;i<l1;i++) 19 { 20 if(a[i]!=b[i]) 21 ans++; 22 } 23 if((ans==1&&l1>=8)||ans==0) 24 { 25 cout<<"yes"<<endl; 26 } 27 else 28 cout<<"no"<<endl; 29 } 30 else if(l1-l2==1&&l1>=8) 31 { 32 int j=0;ans=0; 33 for(int i=0;i<l1;i++) 34 { 35 if(a[i]!=b[j]) 36 { 37 ans++; 38 j--; 39 } 40 j++; 41 } 42 if(ans==1) 43 { 44 cout<<"yes"<<endl; 45 } 46 else 47 { 48 cout<<"no"<<endl; 49 } 50 } 51 else 52 cout<<"no"<<endl; 53 } 54 }
After the data structures exam, students lined up in the cafeteria to have a drink and chat about how much they have enjoyed the exam and how good their professors are. Since it was late in the evening, the cashier has already closed the cash register and does not have any change with him.
The students are going to pay using Jordanian money notes, which are of the following types: 1, 5, 10, 20, 50.
Given how much each student has to pay, the set of notes he’s going to pay with, and the order in which the students arrive at the cashier, your task is to find out if the cashier will have enough change to return to each of the student when they arrive at the cashier.
Input
The first line of input contains a single integer N (1 ≤ N ≤ 105), the number of students in the queue.
Each of the following N lines describes a student and contains 6 integers, K, F1, F2, F3, F4, and F5, where K represents the amount of money the student has to pay, and Fi (0 ≤ Fi ≤ 100) represents the amount of the ith type of money this student is going to give to the cashier.
The students are given in order; the first student is in front of the cashier.
It is guaranteed that no student will pay any extra notes. In other words, after removing any note from the set the student is going to give to the cashier, the amount of money will be less than what is required to buy the drink.
Output
Print yes if the cashier will have enough change to return to each of the students when they arrive in the given order, otherwise print no.
3
4 0 1 0 0 0
9 4 1 0 0 0
8 0 0 1 0 0
我觉得这个代码条理就很清晰。当个模板了。
#include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<string> #include<algorithm> #include<stack> #include<queue> #define ll long long #define inf 0x3f3f3f3f using namespace std; ll money[100005],one[100005],five[100005],ten[100005],twel[100005],fifth[100005]; int main() { ll n; ll num1,num5,num10,num20,num50; while(cin>>n) { for(int i=0; i<n; ++i) scanf("%lld%lld%lld%lld%lld%lld",&money[i],&one[i],&five[i],&ten[i],&twel[i],&fifth[i]); if(one[0] + five[0]*5 + ten[0]*10 + twel[0]*20 + fifth[0]*50 > money[0]) //如果一开始就需要找钱,那直接GG cout<<"no"<<endl; else { num1=0,num5=0,num10=0,num20=0,num50=0; num1 += one[0]; num5 += five[0]; num10 += ten[0]; num20 += twel[0]; num50 += fifth[0]; ll give,flag; for(int i=1; i<n; ++i) { flag = 0; give = (one[i] + five[i]*5 + ten[i]*10 + twel[i]*20 + fifth[i]*50) - money[i]; //计算需要找多少钱 if(give == 0) //如果不用找钱,那直接把收的钱加到总钱当中 { num1 += one[i]; num5 += five[i]; num10 += ten[i]; num20 += twel[i]; num50 += fifth[i]; continue; } else//如果要找钱 { while(num50 > 0 && give >= 50) //要找的钱比50多,且50的钱不为0,就用50的给,直到给不了为止 { num50--; give -= 50; } while(num20 > 0 && give >= 20) //剩下要找的钱比20多,且20的钱不为0,就用20的给,直到给不了为止 { num20--; give -= 20; } while(num10 > 0 && give >= 10) //剩下要找的钱比10多,且10的钱不为0,就用10的给,直到给不了为止 { num10--; give -= 10; } while(num5 > 0 && give >= 5) //剩下要找的钱比5多,且5的钱不为0,就用5的给,直到给不了为止 { num5--; give -= 5; } while(num1 > 0 && give >= 1) //剩下要找的钱比1多,且1的钱不为0,就用1的给,直到给不了为止 { num1--; give--; } if(give > 0) //如过还没找完,表示无法满足要求 { flag = 1; cout<<"no"<<endl; break; } else //否则,表示可以找钱 { num1 += one[i]; num5 += five[i]; num10 += ten[i]; num20 += twel[i]; num50 += fifth[i]; } } } if(!flag) cout<<"yes"<<endl; } } return 0; }
AbdelKader enjoys math. He feels very frustrated whenever he sees an incorrect equation and so he tries to make it correct as quickly as possible!
Given an equation of the form: A1 o A2 o A3 o ... o An = 0, where o is either + or -. Your task is to help AbdelKader find the minimum number of changes to the operators + and -, such that the equation becomes correct.
You are allowed to replace any number of pluses with minuses, and any number of minuses with pluses.
Input
The first line of input contains an integer N (2 ≤ N ≤ 20), the number of terms in the equation.
The second line contains N integers separated by a plus + or a minus -, each value is between 1 and 108.
Values and operators are separated by a single space.
Output
If it is impossible to make the equation correct by replacing operators, print - 1, otherwise print the minimum number of needed changes.
Examples
7
1 + 1 - 4 - 4 - 4 - 2 - 2
3
枚举所有情况,如果是加号,操作数不用加。因为我们一直加的是绝对值,如果条件不成立,返回上一层减去那个数。然后操作数加+1.减号的情况反过来就行了。
#include<iostream> #include<cstring> #include<cstdio> using namespace std; int a[21]; char b[21]; int m; long long int minn=999999999; void dfs(int sum,int t,int s)//sum代表和,t代表递归层数,s代表操作次数。 { if(t==m-1) { if(sum==0) { if(s<minn) minn=s; } return; } if(b[t+1]=='+')//b[i]是为了存储符号数组。 { dfs(sum+a[t+1],t+1,s); dfs(sum-a[t+1],t+1,s+1); } if(b[t+1]=='-') { dfs(sum+a[t+1],t+1,s+1); dfs(sum-a[t+1],t+1,s); } } int main() { cin>>m; long long sum=0; cin>>a[0]; sum=a[0]; for(int i=1;i<m;i++) { cin>>b[i]; getchar(); cin>>a[i]; getchar(); sum+=a[i]; } if(sum%2)//和为奇数时一定不成立。 cout<<-1<<endl; else { dfs(a[0],0,0); if(minn!=999999999) cout<<minn<<endl; else cout<<-1<<endl; } return 0; }