日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POJ 1200 Crazy Search(RK)

發布時間:2023/12/18 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ 1200 Crazy Search(RK) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題意

給定一個由NC個字母組成的字符串,求長度為N的不同子串的個數

思路:

由于只有NC個字母,可以將字母編號,0 ~ NC - 1,轉換成數字,就可以將字符串表示成NC進制的數字,這樣所有字串代表的數字都是唯一的,轉換成10進制的數也是唯一的!

就像10的二進制表示只有1010

例如?

3 4 daababac d = 3 a = 0 b = 1 c = 2 daa = 3 * 4 ^ 2 + 0 * 4 ^ 1 + 0 * 4 ^ 0 = 48 //vs1.0 #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 16000000char str[MAX]; bool hash[MAX]; int ancii[128];int main() {int N, NC;while (scanf("%d%d%s", &N, &NC, str) != EOF) {memset(hash, true, sizeof(hash));memset(ancii, 0, sizeof(ancii));int cnt = 0;for (char *s=str; *s; ++s) {if (!ancii[*s]) {ancii[*s] = ++cnt;if (NC == cnt) break;}}int sum;int ans = 0;int len = strlen(str) - N + 1;for (int i=0; i<len; ++i) {sum = 0;for (int j=0; j<N; ++j) sum = sum * NC + (ancii[str[i+j]] - 1);if (hash[sum]) {++ans;hash[sum] = false;}}printf ("%d\n", ans);}return 0; }//vs2.0 #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 16000000char str[MAX]; bool hash[MAX]; int ancii[128];int main() {int N, NC;while (scanf("%d%d%s", &N, &NC, str) != EOF) {memset(hash, true, sizeof(hash));memset(ancii, 0, sizeof(ancii));int cnt = 0;for (char *s=str; *s; ++s) {if (!ancii[*s]) {ancii[*s] = ++cnt;if (NC == cnt) break;}}int sum = 0;int tmp = 1;for (int i=0; i<N; ++i) {tmp *= NC;sum = sum * NC + ancii[str[i]] - 1; }tmp /= NC;hash[sum] = false;int ans = 1;int len = strlen(str);for (int i=N; i<len; ++i) {sum = (sum - tmp * (ancii[str[i-N]]-1)) * NC + ancii[str[i]] - 1;if (hash[sum]) {++ans;hash[sum] = false;}}printf ("%d\n", ans);}return 0; }

?

轉載于:https://www.cnblogs.com/try86/archive/2012/07/24/2605851.html

總結

以上是生活随笔為你收集整理的POJ 1200 Crazy Search(RK)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。