首页 > 代码库 > 合并字符串问题
合并字符串问题
Do you know how many fools have sacrificed everything in pursuit to be a Neverdie?
As a matter of fact,it‘s much easier than people expect.If one can be an information-based lifeform,
then it can move from medium to medium without losing any integrity. That is to say,
the data that makes up one‘s soul to be a completely separate entity from the vessel that contains it, like software and hardware.
And in this way, Kong Ruili is the first human to achieve immortality, under the help of Xie Yida.
The girl didn‘t recall ever asking for anything that grand.
She just desires to be together with her brother forever--the the girl want to combining her soul with her elder brother, Kong Taoluo.
So Xie picks up the braincase and sets it on his operating table again. It’s a totally new problem for him. How to mix two souls to a new soul?
God knows. It is known for all, soul is coded in numbers and letters, so the greatest genius of the century,
Doctor Xie will solve this problem by following these steps: First, cut Kong Taoluo’s and Ruili’s soul code into some parts whose length is
L (except the last one). Then mix these parts in turn (Ruili’s first): R1K1R2K2…RMKM. As a result of heredity, their soul code is of the same length.
输入
First line contains an integer T, the number of test cases.
Each test case begins with a number L, which means the length of the parts.
The next two lines represent the details of the Ruili’s code and Taoluo’s code(length<=1000).
输出
Output the combined soul code in a line.
样例输入
1
3
WelcomeHomeKongTaoLuo
TogetherTillEndOfTime
样例输出
WelTogcometheHoerTmeKillongEndTaoOfTLuoime
实际上是将两个字符串按每隔L效交替合并
#include "stdafx.h" #include <string> #include <vector> #include <iostream> using namespace std; int main() { int count,len; string s1="",s2=""; vector<char>res;//存放结果 cin>>count; while(count>0) { count--; res.clear(); cin>>len; cin>>s1; cin>>s2; //开始处理 if(len<=0) continue; else{ char temp; int times=min(s1.length(),s2.length())/len;//全整地在两个字符串中存取的次数 for(int i=0;i<times;i++) { for(int j=0;j<len;j++) { res.push_back(s1[len*i+j]); } for(int j=0;j<len;j++) { res.push_back(s2[i*len+j]); } } //剩余的字符串 int s1Len=s1.length(); int s2Len=s2.length(); if(s1Len>=s2Len)//s1串长 { if(s1Len>=(len*times+len))//最后一次在串2中取字符后把串1中剩余部分加在res里 { for(int j=0;j<len;j++) res.push_back(s1[times*len+j]); for(int j=times*len;j<s2Len;j++) res.push_back(s2[j]); for(int j=(times+1)*len;j<s1Len;j++) res.push_back(s1[j]); } else {//最后一次时,两个串都不够len长 for(int j=times*len;j<s1.length();j++) res.push_back(s1[j]); for(int j=times*len;j<s2.length();j++) res.push_back(s2[j]); } } else//字符串2长 { for(int j=times*len;j<s1Len;j++) res.push_back(s1[j]); for(int j=times*len;j<s2Len;j++) res.push_back(s2[j]); } } //输出结果 int resLen=res.size(); for(int i=0;i<resLen;i++) cout<<res[i]; cout<<endl; } //system("pause"); return 0; }
合并字符串问题