HDU 4909 String(組合數學),hdu4909
HDU 4909 String
題目連結
題意:給定一個字串全是小寫字元,可能有一個位置為?,問號可以替代任何字元,也可以刪掉,問有多少連續字串滿足所有字母是偶數個
思路:組合數學,計算所有前最串的各個字母的奇偶狀態,用一個01串表示,然後記錄下個數,對於每個相同的狀態,任選兩個就能得到一個子序列,答案為所有C(num, 2)的和。
但是這個問題多了一個?的情況,但是沒關係,可以枚舉?,然後把序列分為3部分去考慮,?之前,?之後,和包含了?的串分開考慮即可
代碼:
#include <cstdio>#include <cstring>#include <map>using namespace std;typedef int ll;const int N = 20005;int t, n, v, x;char str[N];map<int, int> hash;int main() { scanf("%d", &t); while (t--) {scanf("%s", str);v = -1;n = strlen(str);for (int i = 0; i < n; i++) { if (str[i] == '?') {v = i;break; }}int ans = 0;if (v == -1) { x = 0; hash.clear(); hash[0]++; for (int i = 0; i < n; i++) {x ^= (1<<(str[i] - 'a'));ans += hash[x];hash[x]++; } printf("%d\n", ans); continue;}else { hash.clear(); ans = 0; x = 0; hash[0]++; for (int i = 0; i < v; i++) {x ^= (1<<(str[i] - 'a'));ans += hash[x];hash[x]++; } hash.clear(); x = 0; hash[0]++; for (int i = v + 1; i < n; i++) {x ^= (1<<(str[i] - 'a'));ans += hash[x];hash[x]++; } hash.clear(); x = 0; hash[0]++; for (int i = v + 1; i < n; i++) {x ^= (1<<(str[i] - 'a'));hash[x]++; } x = 0; if (hash.count(x))ans += hash[x]; for (int j = 0; j < 26; j++) {if (hash.count(x^(1<<j))) ans += hash[x^(1<<j)]; } for (int i = v - 1; i >= 0; i--) {x ^= (1<<(str[i] - 'a'));if (hash.count(x)) ans += hash[x];for (int j = 0; j < 26; j++) { if (hash.count(x^(1<<j))) {ans += hash[x^(1<<j)]; }} }}printf("%d\n", ans); } return 0;}
參加acm競賽需要學數論、圖論、組合數學,這三門課在組合數學中有?
離散裡面有一部分的
不過主要的還是自己去學
我也是軟體的 1年前走上了ACM這條不歸路...
組合數學
一般是數學與應用數學專業學的。大學裡的課程基本上都是靠自學,所以要自學也不是不行,只是遇到問題時沒有人可以問這點比較困難,因為本身自己不理解的東西要靠自己會難一點,比較花時間,有些東西看久了還不一定能理解。組合數學裡面有簡單的也有難的,綜合起來還算好。
至於哪個大學,一般這所大學的數學專業好的就行,因為組合數學是數學與應用數學專業的專業課來的。