ZOJ,zoj題目分類

來源:互聯網
上載者:User

ZOJ,zoj題目分類

Description

Little jay really hates to deal with string. But moondy likes it very much, and she's so mischievous that she often gives jay some dull problems related to string. And one day, moondy gave jay another problem, poor jay finally broke out and cried, " Who can help me? I'll bg him! "

So what is the problem this time?

First, moondy gave jay a very long string A. Then she gave him a sequence of very short substrings, and asked him to find how many times each substring appeared in string A. What's more, she would denote whether or not founded appearances of this substring are allowed to overlap.

At first, jay just read string A from begin to end to search all appearances of each given substring. But he soon felt exhausted and couldn't go on any more, so he gave up and broke out this time.

I know you're a good guy and will help with jay even without bg, won't you?

Input

Input consists of multiple cases( <= 20 ) and terminates with end of file.

For each case, the first line contains string A ( length <= 10^5 ). The second line contains an integer N ( N <= 10^5 ), which denotes the number of queries. The next N lines, each with an integer type and a string a ( length <= 6 ), type = 0 denotes substring a is allowed to overlap and type = 1 denotes not. Note that all input characters are lowercase.

There is a blank line between two consecutive cases.

Output

For each case, output the case number first ( based on 1 , see Samples ).

Then for each query, output an integer in a single line denoting the maximum times you can find the substring under certain rules.

Output an empty line after each case.

Sample Input

ab20 ab1 ababababac20 aba1 abaabcdefghijklmnopqrstuvwxyz30 abc1 def1 jmn

Sample Output

Case 111Case 232Case 3110


題意:先給你一個字串,然後給你若干個子串,0代表可以重疊,1代表不能重疊,求出現在母串的次數。
思路:AC自動機,多一個判斷的是如果這個子串與上次出現的次數大於子串長度的話,就代表這次不是重疊的了

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>typedef long long ll;using namespace std;const int maxn = 600010;int wordend[100010];struct Trie {    int nxt[maxn][26], fail[maxn], deep[maxn];    int root, sz;    int cnt[maxn][2], last[maxn];    int newNode() {        for (int i = 0; i < 26; i++)            nxt[sz][i] = -1;        deep[sz++] = 0;        return sz - 1;    }    void init() {        sz = 0;        root = newNode();    }    void insert(char str[], int num) {        int u = root;        for (int i = 0; str[i]; i++) {            int tmp = str[i] - 'a';            if (nxt[u][tmp] == -1)                 nxt[u][tmp] = newNode();            deep[nxt[u][tmp]] = deep[u] + 1;            u = nxt[u][tmp];        }        wordend[num] = u;    }    void build() {        queue<int> q;        fail[root] = root;        int u = root;        for (int i = 0; i < 26; i++) {            if (nxt[u][i] == -1)                nxt[u][i] = root;            else {                fail[nxt[u][i]] = root;                q.push(nxt[u][i]);            }        }        while (!q.empty()) {            u = q.front();            q.pop();            for (int i = 0; i < 26; i++) {                if (nxt[u][i] == -1)                    nxt[u][i] = nxt[fail[u]][i];                else {                    fail[nxt[u][i]] = nxt[fail[u]][i];                    q.push(nxt[u][i]);                }            }        }    }    void query(char *buf) {        for (int i = root; i < sz; i++) {            cnt[i][0] = 0;            cnt[i][1] = 0;            last[i] = -1;        }        int u = root;        for (int i = 0; buf[i]; i++) {            u = nxt[u][buf[i]-'a'];            int tmp = u;            while (tmp != root) {                cnt[tmp][0]++;                if (i - last[tmp] >= deep[tmp]) {                    cnt[tmp][1]++;                    last[tmp] = i;                }                tmp = fail[tmp];            }        }    }} ac;char buf[100010], word[10];int type[100010];int main() {    int n, cas = 1;    while (scanf("%s", buf) != EOF) {        scanf("%d", &n);            ac.init();        for (int i = 0; i < n; i++) {            scanf("%d%s", &type[i], word);            ac.insert(word, i);        }        ac.build();        ac.query(buf);        printf("Case %d\n", cas++);        for (int i = 0; i < n; i++)            printf("%d\n", ac.cnt[wordend[i]][type[i]]);        printf("\n");    }    return 0;}





ZOJ 1049 助

你的程式有一個問題:
在定義前是不能有其它語句的,所以這一點你就錯了。
修改下如下:
#include<stdio.h>
#include<math.h>
int main()
{
int nCases,i;
int n;
double x,y,pi=3.14159 ;//定義語句必須放在最前

scanf("%d",&nCases);
for (i=1;i<=nCases;i++)
{

scanf("%lf %lf",&x,&y); //雙精確度的輸入應為%lf
n=ceil(pi*(x*x+y*y)/100);
printf(" Property %d: This property will begin eroding in year %d.\n",i,n);
}
printf(" END OF OUTPUT. \n");
return 0;
}
 
ZOJ 1049 助

你的程式有一個問題:
在定義前是不能有其它語句的,所以這一點你就錯了。
修改下如下:
#include<stdio.h>
#include<math.h>
int main()
{
int nCases,i;
int n;
double x,y,pi=3.14159 ;//定義語句必須放在最前

scanf("%d",&nCases);
for (i=1;i<=nCases;i++)
{

scanf("%lf %lf",&x,&y); //雙精確度的輸入應為%lf
n=ceil(pi*(x*x+y*y)/100);
printf(" Property %d: This property will begin eroding in year %d.\n",i,n);
}
printf(" END OF OUTPUT. \n");
return 0;
}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.