#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ss second
#define ff first
#define pb push_back
const int N=3e5+5;
int b[N];
int a[N];
map<string,int>mp;
signed main(){
int n;
cin>>n;
string s;
cin>>s;
mp[s]=1;
queue<string>q;
q.push(s);
while(!q.empty()){
string a=q.front();;
q.pop();
bool u=1;
for(int i=0;i<n-1;i++){
if(a[i]==a[i+1]){
u=0;
}
}
if(u){
cout<<mp[a]-1;
return 0;
}
for(int i=0;i<n-1;i++){
string b=a;
swap(b[i],b[i+1]);
if(mp[b]==0 ){
mp[b]=mp[a]+1;
q.push(b);
}
}
}
cout<<-1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
60 ms |
3236 KB |
Output is correct |
6 |
Correct |
2 ms |
436 KB |
Output is correct |
7 |
Correct |
7 ms |
856 KB |
Output is correct |
8 |
Correct |
6 ms |
860 KB |
Output is correct |
9 |
Correct |
41 ms |
2132 KB |
Output is correct |
10 |
Correct |
6 ms |
604 KB |
Output is correct |
11 |
Correct |
95 ms |
3780 KB |
Output is correct |
12 |
Correct |
25 ms |
1628 KB |
Output is correct |
13 |
Correct |
236 ms |
8132 KB |
Output is correct |
14 |
Correct |
24 ms |
1780 KB |
Output is correct |
15 |
Correct |
4 ms |
604 KB |
Output is correct |
16 |
Correct |
13 ms |
852 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
60 ms |
3236 KB |
Output is correct |
6 |
Correct |
2 ms |
436 KB |
Output is correct |
7 |
Correct |
7 ms |
856 KB |
Output is correct |
8 |
Correct |
6 ms |
860 KB |
Output is correct |
9 |
Correct |
41 ms |
2132 KB |
Output is correct |
10 |
Correct |
6 ms |
604 KB |
Output is correct |
11 |
Correct |
95 ms |
3780 KB |
Output is correct |
12 |
Correct |
25 ms |
1628 KB |
Output is correct |
13 |
Correct |
236 ms |
8132 KB |
Output is correct |
14 |
Correct |
24 ms |
1780 KB |
Output is correct |
15 |
Correct |
4 ms |
604 KB |
Output is correct |
16 |
Correct |
13 ms |
852 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Execution timed out |
1045 ms |
164668 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Execution timed out |
1086 ms |
13564 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
60 ms |
3236 KB |
Output is correct |
6 |
Correct |
2 ms |
436 KB |
Output is correct |
7 |
Correct |
7 ms |
856 KB |
Output is correct |
8 |
Correct |
6 ms |
860 KB |
Output is correct |
9 |
Correct |
41 ms |
2132 KB |
Output is correct |
10 |
Correct |
6 ms |
604 KB |
Output is correct |
11 |
Correct |
95 ms |
3780 KB |
Output is correct |
12 |
Correct |
25 ms |
1628 KB |
Output is correct |
13 |
Correct |
236 ms |
8132 KB |
Output is correct |
14 |
Correct |
24 ms |
1780 KB |
Output is correct |
15 |
Correct |
4 ms |
604 KB |
Output is correct |
16 |
Correct |
13 ms |
852 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Execution timed out |
1045 ms |
164668 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |