1. 程式人生 > >UvaLive6439(string使用、回文串)

UvaLive6439(string使用、回文串)

spa out ase amp ons cin case sync style

樣例手寫一寫很容易發現規律(前後一樣的串,則ans+=2),實現起來卻忘了string的便捷性,其實根本用不到哈希。

 1 const int maxn = 1e5 + 5;
 2 int n, ans;
 3 string s, t1, t2;
 4 
 5 int main() {
 6     ios_base::sync_with_stdio(0);
 7     cin.tie(0);
 8     cin >> n;
 9     for (int kase = 1; kase <= n; kase++) {
10         cin >> s;
11 ans = 0; 12 t1 = t2 = ""; 13 int i = 0, j = s.length() - 1; 14 while (i < j) { 15 t1 = t1 + s[i]; 16 t2 = s[j] + t2; 17 if (t1 == t2) { 18 ans += 2; 19 t1 = t2 = ""; 20 } 21 ++i, --j;
22 } 23 if (t1 != t2 || (t1 == t2 && i == j)) ans++; 24 cout << "Case #" << kase << ": " << ans << endl; 25 } 26 return 0; 27 }

UvaLive6439(string使用、回文串)