URAL 2019 Pair: normal and paranormal (STL栈)

题意:在一个半圆内,有2*n个点,其中有大写字母和小写字母。其中你需要连接大写字母到小写字母,其中需要保证这些连接的线段之间没有相交。

如果能够实现,将大写字母对应的小写字母的序号按序输出。

析:我把它看成一个括号序列,然后用栈解决即可。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
  return r >= 0 && r < n && c >= 0 && c < m;
}

struct Node{
  char ch;
  int id;
};
map<int, int> mp;
stack<Node> st;

int main(){
  string s;
  cin >> n >> s;
  int up = 1, lo = 1;
  for(int i = 0; i < s.size(); ++i){
    if(st.empty()){
      if(islower(s[i]))  st.push((Node){s[i], lo++});
      else st.push((Node){s[i], up++});
    }
    else if(islower(s[i])){
      if(s[i] == st.top().ch + 32)  mp[st.top().id] =  lo++, st.pop();
      else  st.push((Node){s[i], lo++});
    }
    else{
      if(s[i] + 32 == st.top().ch)  mp[up++] = st.top().id, st.pop();
      else  st.push((Node){s[i], up++});
    }
  }
  if(!st.empty()){ puts("Impossible");  return 0; }
  for(map<int, int> :: iterator it = mp.begin(); it != mp.end(); ++it)
    if(it == mp.begin())  printf("%d", it->second);
    else  printf(" %d", it->second);
  printf("
");
  return 0;
}

  

原文地址:https://www.cnblogs.com/dwtfukgv/p/6789658.html