POJ 1200 Crazy Search(RK)
生活随笔
收集整理的這篇文章主要介紹了
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)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: npm install 安装软件,出现
- 下一篇: ORA-01940 无法删除当前已连接的