We have a string originalWord. Each character of originalWord is either 'a' or 'b'. Timmy claims that he can convert it to finalWord using exactlyk moves. In each move, he can either change a single 'a' to a 'b', or change a single 'b' to an 'a'.
You are given the strings originalWord and finalWord, and the int k. Determine whether Timmy may be telling the truth. If there is a possible sequence of exactly k moves that will turn originalWord into finalWord, return "POSSIBLE" (quotes for clarity). Otherwise, return "IMPOSSIBLE".
Definition
Class:
EasyConversionMachine
Method:
isItPossible
Parameters:
string, string, int
Returns:
string
Method signature:
string isItPossible(string originalWord, string finalWord, int k)
(be sure your method is public)
Notes
-
Timmy may change the same letter multiple times. Each time counts as a different move.
Constraints
-
originalWord will contain between 1 and 50 characters, inclusive.
-
finalWord and originalWord will contain the same number of characters.
-
Each character in originalWord and finalWord will be 'a' or 'b'.
-
k will be between 1 and 100, inclusive.
Examples
0)
"aababba"
"bbbbbbb"
2
Returns: "IMPOSSIBLE"
It is not possible to reach finalWord in fewer than 4 moves.
1)
"aabb"
"aabb"
1
Returns: "IMPOSSIBLE"
The number of moves must be exactly k=1.
2)
"aaaaabaa"
"bbbbbabb"
8
Returns: "POSSIBLE"
Use each move to change each of the letters once.
3)
"aaa"
"bab"
4
Returns: "POSSIBLE"
The following sequence of 4 moves does the job: aaa -> baa -> bab -> aab -> bab
4)
"aababbabaa"
"abbbbaabab"
9
Returns: "IMPOSSIBLE"
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
class EasyConversionMachine{ public: string isItPossible(string originalWord, string finalWord, int k) { int n=originalWord.size(); int tot=0; for (int i=0;i<n;i++) if (originalWord[i]!=finalWord[i]) tot++; if (tot<=k && (k-tot)%2==0) //鍌誨弶錛岃繖閲屽垰鍒氬紑濮嬫悶鍙嶄簡錛宼esting WA浜嗕竴嬈?br /> return"POSSIBLE"; return"IMPOSSIBLE"; } };
int cmp(student a,student b) { if (a.AC>b.AC) return1; if (a.AC==b.AC&&a.time<b.time) return1; int i=0; while (a.AC==b.AC&&a.time==b.time&&i<strlen(a.name)&&i<strlen(b.name)) { if (a.name[i]<b.name[i]) return1; i++; } return0; }
int qsort(int l,int r) //鑷繁榪樻槸涓嶄範(fàn)鎯潯鐢╯tl閲岀殑sort() { int i,j; i=l; j=r; temp=p[(i+j)/2]; while (i<=j) { while (i<=j&&cmp(p[i],temp)) i++; while (i<=j&&cmp(temp,p[j])) j--; if (i<=j) { tt=p[i]; p[i]=p[j]; p[j]=tt; i++; j--; } } if (l<j) qsort(l,j); if (i<r) qsort(i,r); return0; }
int main() { int n,m,i,j,tmp; char ch; scanf("%d%d",&n,&m); tot=0; while (scanf("%s",&p[0].name)==1&&p[0].name[0]!='0') { tot++; p[tot]=p[0]; p[tot].AC=0; p[tot].time=0; for (i=1; i<=n ; i++ ) { scanf("%d",&tmp); if (tmp>0) { p[tot].AC++; p[tot].time+=tmp; ch=getchar(); if (ch=='(') { scanf("%d",&tmp); p[tot].time+=m*tmp; getchar(); } } } }