UvaLive6439(string使用、回文串)

样例手写一写很容易发现规律(前后一样的串,则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 }

 

posted @ 2019-03-23 21:44  AlphaWA  阅读(157)  评论(0编辑  收藏  举报