| ||||||||||
Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
输入格式的疑问,为什么这样错了?大神们请指教小弟之前写的代码如下: 用的是getline提取字符串来提取数据。 #include <iostream> #include <string> #include <sstream> #include <stack> using namespace std; bool TrySolve(string line); /*if can be solved ,return false,else return true;*/ int a[1001]; int max_length = 0; int main() { // freopen("input.txt","r",stdin); string line = ""; int flag = 0; while(getline(cin,line)) { max_length = line[0] - '0'; while(getline(cin,line)) { if (line.length() > 1) { if (flag) { cout << endl; } if (TrySolve(line)) { cout << "No" << endl; } else { cout << "Yes" << endl; } continue; } else if (line.length() == 1 && line[0] == '0') { flag = 1; break; } else cout << "Yes" << endl; } } return 0; } bool TrySolve(string line) { stringstream ss(line); int tmp = 0; int i = 0; int b[1001]; stack<int> sta_var; while(ss >> a[i++]); for (int i = 0; i < max_length; ++i) { b[i] = i+1; } int cur = 0 ; int j = 0; for (int i = 0;i < max_length; ++i) { //search from the top element of the stack if (!sta_var.empty() && sta_var.top()==a[i]) { sta_var.pop(); continue; } // search from the current position for (j = cur; j < max_length; ++j) { if (b[j] == a[i]) { cur = a[i]; break; } // can't find ,then push into the stack else { sta_var.push(b[j]); } } if (j == max_length) { return true; } } return false; } //这个代码始终就是wa,不过可以找到的测试数据都可以通过。不过换了一下输入,核心算法没变,就ac了,请大神们指教,贴出代码如下: /*Wrote by Dream Chen */ #include <iostream> #include <string> #include <stack> using namespace std; bool TrySolve(); /*if can be solved ,return false,else return true;*/ int a[1001]; int max_length = 0; int main() { // freopen("input.txt","r",stdin); string line = ""; int flag = 0; while(cin >> max_length) { if (max_length == 0) { break; } int temp = 0; while(cin >> temp) { if (temp) { a[0] = temp; for (int i = 1; i < max_length; ++i) { cin >> a[i]; } if (!TrySolve()) { cout << "Yes" << endl; } else cout << "No" << endl; } else { cout << endl; break; } } } return 0; } bool TrySolve() { stack<int> sta_var; int b[1001]; for (int i = 0; i < max_length; ++i) { b[i] = i+1; } int cur = 0 ; int j = 0; for (int i = 0;i < max_length; ++i) { //search from the top element of the stack if (!sta_var.empty() && sta_var.top()==a[i]) { sta_var.pop(); continue; } // search from the current position for (j = cur; j < max_length; ++j) { if (b[j] == a[i]) { cur = a[i]; break; } // can't find ,then push into the stack else { sta_var.push(b[j]); } } if (j == max_length) { return true; } } return false; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator