Problem Description
Bruce Force has had an interesting idea how to encode strings. The following is the description of how the encoding is done:
Let x1,x2,...,xn be the sequence of characters of the string to be encoded.
For example, when we want to encode the string "hello", and we choose the value m = 3 and the permutation 2, 3, 1, 5, 4, the data would be encoded in 3 steps: "hello" -> "elhol" -> "lhelo" -> "helol".
Bruce gives you the encoded strings, and the numbers m and p1, ..., pn used to encode these strings. He claims that because he used huge numbers m for encoding, you will need a lot of time to decode the strings. Can you disprove this claim by quickly decoding the strings?
Input
The input contains several test cases. Each test case starts with a line containing two numbers n and m (1 ≤ n ≤ 80, 1 ≤ m ≤ 109). The following line consists of n pairwise different numbers p1,...,pn (1 ≤ pi ≤ n). The third line of each test case consists of exactly n characters, and represent the encoded string. The last test case is followed by a line containing two zeros.
Output
For each test case, print one line with the decoded string.
Sample Input
5 3
2 3 1 5 4
helol
16 804289384
13 10 2 7 8 1 16 12 15 6 5 14 3 4 11 9
scssoet tcaede n
8 12
5 3 4 2 1 8 6 7
encoded?
0 0
Sample Output
hello
second test case
encoded?
代码实现:
#include
#include
#include
using namespace std;
class Solution{
public:
bool Isomorphic(string s1,string s2)
{
int m1[128]={0};
int m2[128]={0};
for(int i=0;i<s1.size()-1;i++)
{
if(m1[s1[i]]!=m2[s2[i]]){
return false;
}else{
m1[s1[i]]=i+1;
m2[s2[i]]=i+1;
}
}
return true;
}
};
int main()
{
string s1="abb";
string s2="cdd";
int ans;
Solution ob;
ans=ob.Isomorphic(s1,s2);
printf("%d",ans);
system("pause");
return 0;
}