B. Little Girl and Game
time limit per test
2 secondsmemory limit per test
256 megabytesinput
standard inputoutput
standard outputThe Little Girl loves problems on games very much. Here's one of them.
Two players have got a string s, consisting of lowercase English letters. They play a game that is described by the following rules:
- The players move in turns; In one move the player can remove an arbitrary letter from string s.
- If the player before his turn can reorder the letters in string s so as to get a palindrome, this player wins. A palindrome is a string that reads the same both ways (from left to right, and vice versa). For example, string "abba" is a palindrome and string "abc" isn't.
Determine which player will win, provided that both sides play optimally well — the one who moves first or the one who moves second.
Input
The input contains a single line, containing string s (1 ≤ |s| ≤ 103). String s consists of lowercase English letters.
Output
In a single line print word "First" if the first player wins (provided that both players play optimally well). Otherwise, print word "Second". Print the words without the quotes.
Sample test(s)
Input
aba
Output
First
Input
abca
Output
Second
首先注意,题目中说明字母的顺序可以打乱。即是说,abb可以重新排序成bab,构成回文,此时先手胜。
回文序列实际上是左右对称的,因此频数为奇数的不同字母的个数必小于或等于1个,且如果频数为奇数的字母存在,必位于正中间,如aabaa,abcba。
反之,一个字母序列中,频数为奇数的不同字母的个数小于或等于1,则必能通过重新排序变成回文序列,如abab,abbac。因此,游戏结束时就是字母
序列中频数为奇数的不同字母的个数小于或等于1时。
假设原来字母序列中,频数为奇数的不同字母的个数为odd。
如果odd为0,已是回文,先手胜。
如果odd为奇数,先手只需每次都去掉频数为奇数的字母即可获胜。因为先手每如此操作一次,odd减一,如果后手也如此,由于odd为奇数,故而先使得
odd==1的是后手,则先手胜。如果后手不如此操作,即是去掉频数为偶数的字母,在后手操作后,odd只增不减,不可能为1.
如果odd为偶数,后手必胜。理由同上。
AC Code:
1 #include <iostream> 2 #include <string> 3 #include <set> 4 #include <map> 5 #include <vector> 6 #include <stack> 7 #include <queue> 8 #include <cmath> 9 #include <cstdio> 10 #include <cstring> 11 #include <algorithm> 12 using namespace std; 13 #define LL long long 14 #define cti const int 15 #define dg(i) cout << "*" << i << endl; 16 17 char s[1003]; 18 int cnt[27], odd; 19 20 int main() 21 { 22 while(scanf("%s", s) != EOF) 23 { 24 odd = 0; 25 memset(cnt, 0, sizeof(cnt)); 26 for(int i = 0; s[i] != '\0'; i++) 27 cnt[s[i]-'a']++; 28 for(int i = 0; i < 26; i++) 29 odd += (cnt[i] & 1); 30 if(odd & 1 || !odd) puts("First"); 31 else puts("Second"); 32 } 33 return 0; 34 }