hdu 1022 AND toj 1036 栈的运用

其实就是模拟一下栈啦。

hdu 1022:

 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 }

 toj 1036(和上一个题基本一样,输入输出麻烦点):

 1 #include <cstdio>

 2 

 3 const int N = 1000;

 4 int s[N];

 5 

 6 int main ()

 7 {

 8     int n;

 9     while ( scanf("%d", &n), n )

10     {

11         int tmp;

12         while ( scanf("%d", &tmp), tmp )

13         {

14             int top = 0, p = 1;

15             bool flag = true;

16             for ( int i = 0; i < n; i++ )

17             {

18                 if ( top == 0 )

19                 {

20                     s[top++] = p++;

21                 }

22                 while ( s[top - 1] != tmp )

23                 {

24                     if ( p == n + 1 ) break;

25                     s[top++] = p++;

26                 }

27                 if ( s[top - 1] != tmp )

28                 {

29                     flag = false;

30                 }

31                 else

32                 {

33                     top--;

34                 }

35                 if ( i != n - 1 ) 

36                 {

37                     scanf("%d", &tmp);

38                 }

39             }

40             printf("%s\n", flag ? "Yes" : "No");

41         }

42         putchar('\n');

43     }

44     return 0;

45 }

你可能感兴趣的:(HDU)