字符串的分割

        基因工程总部需要让M个国家共同解码一段长度为M*L的基因序列。总部的想法很简单,把基因序列均匀分成连续的M段,每个国家各自选一段进行处理,各自的选择不允许重复。但是这些基因太长了,总部文员不知怎么办,请你帮他将序列分段并交给它们对应的国家。

输入
        包括3行,第一行包含两个数M,L(1≤M≤100,1≤L≤1000)
        第二行为长度为M*L的字符串,保证字符串只包含A,T,C,G四个大写字母。
        第三行包含M个数,其中的第i个数Xi表示第i个国家选了从左向右数的第Xi段基因序列,保证Xi互不相同且为1~M的排列。

输出
         输出M行,第i行表示第i个国家选的基因序列。
         不要在行末输出任何多余的空格或字符。

输入样例 1

3 3
ACCTGACAG
2 3 1

输出样例 1

TGA
CAG
ACC

c++代码

#include<iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;

int main()
{
    int m, l, a[101];
	string wholeStr, tempStr;
	cin >> m >> l;
	cin >> wholeStr;
	for (int i = 1; i <= m; i++) {
		cin >> a[i];
	}
	for (int i = 1; i <= m; i++) {
		int startPosition = (a[i] - 1) * l;                //找到第a[i]段字符串的起始下标
		tempStr = wholeStr.substr(startPosition, l);       //把从下标a[i]开始的长度为l的子串赋值给tempStr
		cout << tempStr << endl;
	}
    return 0;
}
posted on 2021-06-12 10:20  雾恋过往  阅读(154)  评论(0编辑  收藏  举报

Live2D