第一行输入一个整数
表示字符串及排列的长度。
第二行输入一个长度为
,仅由
和
构成的字符串
。
如果不存在满足条件的排列,直接输出
;否则,在一行上输出
个整数
表示你构造出的排列。
如果存在多个满足条件的排列,输出任意一个均可,系统将自动判定其正确性。请注意,自测运行功能可能因此返回错误结果,请自行检查答案正确性。
3 001
3 1 2
对于这个样例,
由于
,排列的前一项元素无法构成一个排列;
由于
,排列的前两项元素无法构成一个排列;
由于
,排列的前三项元素构成一个排列;
同时,输出
、
等答案也都是合法的。
4 1110
-1
在此样例中,若存在合法排列,则前三位必须依次形成排列,但第四位又要求整体不形成排列,显然不可能,因此答案为。
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String length = in.nextLine();
String input = in.nextLine();
resultList = new LinkedList<>();
boolean res = getResult(input);
if (res) {
resultList.forEach(s->System.out.print(s + " "));
} else {
System.out.println(-1);
}
}
static List<Integer> resultList = new LinkedList<>();
public static boolean getResult(String str) {
if (str.endsWith("0")) {
return false;
}
Integer temp = 1;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == '0') {
resultList.add(i + 2);
} else {
resultList.add(temp);
temp = i + 2;
}
}
return true;
} import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); // 读取字符串长度
String s = sc.next(); // 读取01字符串
sc.close(); // 关闭扫描器
// 若字符串最后一位不是1,直接输出-1(无法满足置换要求)
if (s.charAt(n - 1) != '1') {
System.out.println(-1);
return;
}
int flag = 0; // 标记下一个置换区间的起始位置
int[] arr = new int[n]; // 存储置换结果的数组
// 遍历字符串,处理每个'1'对应的置换
for (int i = 0; i < n; i++) {
if (s.charAt(i) == '1') {
// '0'对应的位置:值=自身索引+1(不置换)
for (int j = flag + 1; j < i; j++) {
arr[j] = j + 1;
}
// '1'对应的位置:flag和i互换(值=对方索引+1)
arr[flag] = i + 1;
arr[i] = flag + 1;
flag = i + 1; // 更新下一个置换区间起点
}
}
// 输出最终置换结果
for (int a : arr) {
System.out.print(a + " ");
}
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n=in.nextInt();
String s=in.next();
Stack<Integer> st=new Stack<Integer>();
if(s.charAt(n-1)=='0'){
System.out.print(-1);
return;
}
for(int i=1;i<=n;i++){
st.push(i);
if(s.charAt(i-1)!='0'){
while(!st.empty()) System.out.print(st.pop()+" ");
}
}
}
} #include <algorithm>
#include <iostream>
#include <bits/stdc++.h>
#include <vector>
using namespace std;
int main() {
int n;cin>>n;
string s;cin>>s;
if(s[n-1]=='0'){
cout<<-1<<endl;
return 0;
}
vector<int>p(n);
for(int i=0;i<n;i++){
p[i]=i+1;
}
for(int i=0;i<n;i++){
if(s[i]=='0')swap(p[i],p[i+1]);
cout<<p[i]<<" ";
}
}
import sys
n=int(input())
s=list(map(int,input()))
if s[-1]==0:
print("-1")
sys.exit()
num_list=[i for i in range(1,n+1)]
output_list=[0 for i in range(1,n+1)]
count=0
for idx,flag in enumerate(s):
if flag==1 and idx==0:
output_list[0]=1
elif idx>=1 and flag==1 and s[idx-1]==1:
output_list[idx]=idx+1
elif flag==0:
count+=1
elif flag==1 and count>0:
output_list[idx-count:idx]=[i for i in range(idx+1,idx-count,-1)]
count=0
print(' '.join(map(str,output_list[:n]))) import sys # 可以先构造一个元素为 1 到 n 的数组 nums # 然后遍历 s 的每一位字符 # 如果 s[i] = 1, nums[i] 就保持不变;如果 s[i] = 0, 就交换 nums[i] 和 num[i+1] # 最后一个数字 nums[end] 无法再往后找数字交换了,所以 s[end] 必须 = 1 # 上面的方法空间复杂度是 O(n), 去掉 nums 数组,空间复杂度就是 O(1) 了 n = int(input()) s = input().strip() if s[-1] == '0': print(-1) sys.exit() prev = 0 for i in range(n): current = i + 1 if s[i] == '1': if prev == 0: # 如果前面没有数字被换到这里,就输出当前的数字 print(current, end=' ') else: # 如果前面的数字被换到这里了,输出前面的数字 print(prev, end=' ') prev = 0 else: # 遇到 0 了, 先把后面的数字输出 print(current + 1, end=' ') if prev == 0: # 把当前数字存起来,后面再输出 prev = i + 1
#include <cstddef>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
string s;
cin>>n>>s;
if(n>100000){cout<<"-1";return 0;}
vector<int> v,v1;
if(s[n-1]=='0'){cout<<"-1";return 0;}//末尾必为1
else{
int pos1=0;
while(s.find('1')!=string::npos){
int pos=s.find('1')+1;//新1的位置
v.push_back(pos+pos1);//将1的位置存入v
s.erase(0,pos);
pos1+=pos;
}
int y=0;
for(int i:v){
for(int j=i;j>y;j--){
v1.push_back(j);
}
y=i;
}
if(s.size()){
for(int i=n;i>y;i--){
v1.push_back(i);
}
}
for(int i:v1){
cout<<i<<" ";
}
}
}
// 64 位输出请用 printf("%lld") #include <iostream>
#include <vector>
using namespace std;
int main() {
string s;
int n;
cin >> n >> s;
vector<int>p(n);
if (s.back() == '0')cout << -1;
else {
int N = n;
for (int i = n - 1; i >= 0; i--) {
if (s[i] == '1') {
int j = i - 1;
while (j >= 0 && s[j] == '0') j--;
for (int k = j + 1; k <= i; k++)p[k] = N--;
}
}
for (int i = 0; i < n; i++) {
if (i) cout << ' ';
cout << p[i];
}
}
} #include <stdio.h>
#include <stdlib.h>
//让我判断是不是匹配还好说,让我构造??
//只能找规律,再想五分钟想不出来就老实看答案。
//好像找到规律了,遇到1则填对应的数字即可,遇到0则和下一个是1的位置交换数字即可,交换完之后就跳过下一个1的位置,不过这种只适合有解的情况啊。如果后面找不到有1的情况,则就当无解把
int main() {
int n;
scanf("%d",&n);
char *string=malloc(sizeof(char)*(n+1));
scanf("%s",string);
//去掉字符串的0位置,然后末尾的'\0'也不要了,这样结果才能一一对应
for(int i=n;i>0;i--)
{
string[i]=string[i-1];
}
int *result=malloc(sizeof(int)*(n+1));
for(int i=0;i<=n;i++) //初始结果数组为递增数组,但是后面元素下标为0的数字不用
{
result[i]=i;
}
//然后和字符串一一对应的了,找能交换的位置了
for (int i=1; i<=n; ) {
if(string[i]=='1') //遇到1则结果数组保持原样
{
i++;
}else if(string[i]=='0') //遇到0则找下一个为1的位置交换
{
int a=i;//记录当前位置
while(string[i]=='0') //跳过所有0的位置
{
i++;
if (i>n) { //如果找完后续所有位置都没有1的位置,则无解
printf("-1");
return 0;
}
}
//来到了string[i]=='1'的位置,和a的职位交换
int temp=result[a];
result[a]=result[i];
result[i]=temp; //交换
i++; //移动到下一个位置继续探索
}
}
//输出result数组的所有结果
for(int i=1; i<=n; i++)
{
printf("%d ",result[i]);
}
return 0;
} n = int(input())
s = input()
ls = [i for i in range(1,n+1)]
i = 0
flag = True
if s[-1] == '0':
flag = False
while i<n and flag:
if s[i] == '0':
nex = s.find('1',i)
if nex==None:
flag = False
break
tmp = ls[i]
ls[i] = ls[nex]
ls[nex] = tmp
i = nex
continue
i += 1
if flag:
for x in ls:
print(x,end=' ')
else:
print(-1) using System;
using System.Collections.Generic;
using System.Linq;
public class Program {
public static void Main() {
var n = int.Parse(Console.ReadLine ());
var strList = Console.ReadLine ().Select(x => int.Parse(x.ToString())).ToList();
var numberList = Enumerable.Range(1, n).ToList();
if (strList.Last() == 0) {
Console.WriteLine(-1);
return;
}
for (int i = 0; i < strList.Count - 1; i++) {
if (strList[i] == 0) { // 如果=0,则和数字的后面一位交换位置
int temp = numberList[i];
numberList[i] = numberList[i + 1];
numberList[i + 1] = temp;
}
}
Console.WriteLine(string.Join(" ", numberList));
}
} 控制台传过来的字符串为str,①无结果:当str的结尾为0时,说明不可能有满足题意的字符顺序,直接输出-1。
②有结果:遇到1时再填充字符,两层for循环,外层right从前到后寻找字符“1”,
内层j从上一个字符“1”后开始遍历到外层找到的1,填充为j + 2(下标比当前位置靠后。比如第二个位置,我们输入3),
遍历到达“1”(right)时,输出left + 1的值,再把left赋值为right + 1。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
int lens = in.nextInt();
in.nextLine();
String str = in.nextLine();
char[] chars = str.toCharArray();
if (chars[chars.length - 1] == '0'){
System.out.println(-1);
}else{
int impossible = 2;
int possible = 1;
int left = 0;
for (int right = 0; right < chars.length; right++){
if (chars[right] == '1'){
for (int j = left; j < right; j++){
System.out.print(j + 2 + " ");
}
System.out.print(left + 1 + " ");
left = right + 1;
}
}
}
}
}
} 题目含义:
(1)若 ,则数列 {
,...,
} 必须由数字 1 ~ {i+1} 组成,且每个数字只出现一次,其中 i 从1开始;
(2)若,则表示数列 {
,...,
} 至少有一个元素大于数字 i。
'''
n = int(input())
s = input()
if s[-1] == '0':
print(-1)
else:
res = [i for i in range(1, n+1)]
for i in range(n):
if s[i] == '0':
res[i], res[i+1] = res[i+1], res[i]
print(' '.join(map(str, res)))
'''