UVA 11988

来源:互联网 发布:生化危机6知乎 编辑:程序博客网 时间:2024/04/30 21:41

简单的题,要用链表,保持时间复杂度在O(n),切记不要用数组、字符串,这道题目的时间卡的很紧,多次赋值会超时的。

源代码如下:

#include<iostream>#include<vector>#include<string>#include<set>#include<stack>#include<queue>#include<map>#include<algorithm>#include<iomanip>using namespace std;typedef struct node{char data;struct node*next;node(char a){data = a;next = NULL;}}node;int main(){string s;while (getline(cin, s)){int j;for (j = 0; j < s.size(); j++){if (s[j] != '['&&s[j] != ']') break;}node *head = new node(s[j]);node *tail = head;for (int i = j+1; i < s.size();){if (s[i] == '['){while (i < s.size()&&s[i]=='['){i++;}if (i == s.size()) break;if (s[i] != ']'){node* newhead = new node(s[i]);node* temp = newhead;i++;while (i < s.size() && s[i] != '['&&s[i] != ']'){temp->next = new node(s[i]);i++;temp = temp->next;}temp->next = head;head = newhead;}}else if (s[i] == ']'){while (i < s.size() && s[i] == ']') i++;if (s[i] != '['){while (i < s.size() && s[i] != '['&&s[i] != ']'){tail->next = new node(s[i]);i++;tail = tail->next;}}}else{tail->next = new node(s[i]);tail = tail->next;i++;}}while (head != NULL){cout << head->data;head = head->next;}cout << endl;}//system("pause");return 0;}

原创粉丝点击