这个题就是细致一些,别把行列弄混了就成。我做法可能有点麻烦,就是用一个类 来表示key串的每个字符以及相应的pos ,然后在进行行列填充就行了; 开始把行列搞错了弄了好长时间。。QAQ
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
class CC{
public:
int r;
char ch;
} ;
bool cmp(CC a,CC b)
{
if(a.ch==b.ch) return a.r<b.r;
return a.ch<b.ch;
}
CC c[105];
string key;
string s,ss;
char ma[105][105];
int main()
{
while(cin>>key)
{
if(key=="THEEND") break;
cin>>s;
int len1=key.length();
int len2=s.length();
int row= len2/len1;
for (int i=0;i<len1;i++)
{
c[i].ch=key[i];
c[i].r=i;
}
stable_sort(c,c+len1,cmp);
for(int i=0;i<len2;i+=row)
{
ss=s.substr(i,row);
for(int j=0;j<row;j++)
{
ma[j][c[i/row].r] =ss[j];
}
}
for(int i=0;i<row;i++)
for(int j=0;j<len1;j++)
cout<<ma[i][j];
cout<<endl;
}
}