其实就是模拟一下栈啦。
1 #include <iostream> 2 using namespace std; 3 4 const int N = 10; 5 char o1[N]; 6 char o2[N]; 7 char s[N]; 8 int ans[N * 2]; 9 10 int main () 11 { 12 int n; 13 while ( cin >> n ) 14 { 15 cin >> o1 >> o2; 16 int top = 0, p = 0, cnt = 0; 17 for ( int i = 0; i < n; i++ ) 18 { 19 if ( top == 0 ) 20 { 21 s[top++] = o1[p++]; 22 ans[cnt++] = 1; 23 } 24 while ( s[top - 1] != o2[i] ) 25 { 26 if ( p == n ) break; 27 s[top++] = o1[p++]; 28 ans[cnt++] = 1; 29 } 30 if ( s[top - 1] != o2[i] ) 31 { 32 break; 33 } 34 else 35 { 36 top--; 37 ans[cnt++] = 0; 38 } 39 } 40 if ( cnt == 2 * n ) 41 { 42 cout << "Yes." << endl; 43 for ( int i = 0; i < cnt; i++ ) 44 { 45 cout << ( ans[i] ? "in" : "out" ) << endl; 46 } 47 } 48 else 49 { 50 cout << "No." << endl; 51 } 52 cout << "FINISH" << endl; 53 } 54 return 0; 55 }
时间: 2024-12-25 04:16:29