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 |
找到错误了 把cin 改称 gets 就过了,用string不能直接输入空串吗, 我先gets 然后付给string有没有好点的#include <iostream> #include <iomanip> #include <cmath> #include <string> using namespace std; #define min(a, b) ((a) < (b) ? (a):(b)) string a, b; string insa; string insb; bool isPalindrome(string& s, string& ins, bool tail) { int i, j; if(tail) { for(i = ins.length(), j = s.length() - 1; i < j; i ++, j --) { if(s[i] != s[j]) break; } if(i >= j) { return true; } } else { string temps = s + ins; for(i = 0, j = temps.length() - 1; i < j; i ++, j --) { if(temps[i] != temps[j]) break; } if(i >= j) { return true; } } return false; } bool ok() { bool aaok = isPalindrome(a, insa, true); bool baok = isPalindrome(b, insa, false); bool abok = isPalindrome(a, insb, false); bool bbok = isPalindrome(b, insb, true); bool a = (aaok && !baok) || (!aaok && baok); bool b = (abok && !bbok) || (!abok && bbok); if(a && !b) { cout << insa << endl; return true; } else if(!a && b) { cout << insb << endl; return true; } else if(a && b) { cout << min(insa, insb) << endl; return true; } else { return false; } } void main() { char chara[2000], charb[2000]; while(gets(chara) && gets(charb)) { int i; a = chara; b = charb; if(a == b) cout << "No Solution." << endl; else { int lena = a.length(); int lenb = b.length(); if(lena > lenb) { int ltemp = lena; lena = lenb; lenb = ltemp; string stemp = a; a = b; b = stemp; } insa = ""; insb = ""; for(i = 0; i <= lena; i ++) { if(ok()) { break; } if(i < lena) { insa.insert(0, a[i]); insb.insert(0, b[i]); } } if(i == lena + 1) { if(b[lena] == 'a') { insa.insert(0, 'b'); cout << insa << endl; } else { insa.insert(0, 'a'); cout << insa << endl; } } } } } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator