2015 Multi-University Training Contest 8 (莫对算法+AC自动机+DP)

Danganronpa

Problem Description

Danganronpa is a video game franchise created and developed by Spike Chunsoft, the series’ name is compounded from the Japanese words for “bullet” (dangan) and “refutation” (ronpa).

Now, Stilwell is playing this game. There are n verbal evidences, and Stilwell has m “bullets”. Stilwell will use these bullets to shoot every verbal evidence.

Verbal evidences will be described as some strings Ai, and bullets are some strings Bj. The damage to verbal evidence Ai from the bullet Bj is f(Ai,Bj).
f(A,B)=∑i=1|A|−|B|+1[ A[i…i+|B|−1]=B ]
In other words, f(A,B) is equal to the times that string B appears as a substring in string A.
For example: f(ababa,ab)=2, f(ccccc,cc)=4

Stilwell wants to calculate the total damage of each verbal evidence Ai after shooting all m bullets Bj, in other words is ∑mj=1f(Ai,Bj).

Input

The first line of the input contains a single number T, the number of test cases.
For each test case, the first line contains two integers n, m.
Next n lines, each line contains a string Ai, describing a verbal evidence.
Next m lines, each line contains a string Bj, describing a bullet.

T≤10
For each test case, n,m≤105, 1≤|Ai|,|Bj|≤104, ∑|Ai|≤105, ∑|Bj|≤105
For all test case, ∑|Ai|≤6∗105, ∑|Bj|≤6∗105, Ai and Bj consist of only lowercase English letters

Output

For each test case, output n lines, each line contains a integer describing the total damage of Ai from all m bullets, ∑mj=1f(Ai,Bj).

Sample Input

1
5 6
orz
sto
kirigiri
danganronpa
ooooo
o
kyouko
dangan
ronpa
ooooo
ooooo

Sample Output

1
1
0
3
7

对下面的串建立AC自动机,然后去匹配就行了

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=100010;
const int maxm=10010;
const int maxnode=600010;
const int MOD=1e9+7;
const int SIGMA_SIZE=26;
const int INF=0x3f3f3f3f;
int N,M;
char a[maxn][maxm];
struct AC{
    int ch[maxnode][SIGMA_SIZE];
    int fail[maxn],val[maxn],last[maxn];
    int sz;
    void clear(){
        memset(ch[0],0,sizeof(ch[0]));
        sz=1;
    }
    int idx(char x){
        return x-'a';
    }
    void insert(char *s){
        int u=0;
        for(int i=0;s[i];i++){
            int c=idx(s[i]);
            if(!ch[u][c]){
                memset(ch[sz],0,sizeof(ch[sz]));
                val[sz]=0;
                ch[u][c]=sz++;
            }
            u=ch[u][c];
        }
        val[u]++;
    }
    void getfail(){
        queue<int> q;
        int u=0;
        fail[u]=0;
        for(int c=0;c<SIGMA_SIZE;c++){
            u=ch[0][c];
            if(u){
                fail[u]=last[u]=0;
                q.push(u);
            }
        }
        while(!q.empty()){
            int r=q.front();
            q.pop();
            for(int c=0;c<SIGMA_SIZE;c++){
                u=ch[r][c];
                if(!u){
                    ch[r][c]=ch[fail[r]][c];
                    continue;
                }
                q.push(u);
                fail[u]=ch[fail[r]][c];
                last[u]=val[fail[u]]?fail[u]:last[fail[u]];
            }
        }
    }
    int solve(char s[]){
        int u=0;
        int sum=0;
        for(int i=0;s[i];i++){
            int c=idx(s[i]);
            u=ch[u][c];
            int tmp=0;
            if(val[u])tmp=u;
            else if(last[u])tmp=last[u];
            while(tmp){
                sum+=val[tmp];
                tmp=last[tmp];
            }
        }
        return sum;
    }
}ac;
char s[10100];
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&N,&M);
        for(int i=1;i<=N;i++){
            scanf("%s",a[i]);
        }
        ac.clear();
        for(int i=1;i<=M;i++){
            scanf("%s",s);
            ac.insert(s);
        }
        ac.getfail();
        int ans=0;
        for(int i=1;i<=N;i++){
            ans=ac.solve(a[i]);
            printf("%d\n",ans);
        }
    }
    return 0;
}

The path

Problem Description

You have a connected directed graph.Let d(x) be the length of the shortest path from 1 to x.Specially d(1)=0.A graph is good if there exist x satisfy d(1)

Input

There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains two integers n and m,the number of vertexs and the number of edges.Next m lines contain two integers each, ui and vi (1≤ui,vi≤n), indicating there is a link between nodes ui and vi and the direction is from ui to vi.

n3105,m6105
1n,m105

Output

For each test case,print m lines.The i-th line includes one integer:the length of edge from ui to vi

Sample Input

2
4 6
1 2
2 4
1 3
1 2
2 2
2 3
4 6
1 2
2 3
1 4
2 1
2 1
2 1

Sample Output

1
2
2
1
4
4
1
1
3
4
4
4

这里写图片描述

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=300010;
const int maxm=1010;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;
int N,M;
int dis[maxn];
int vis[maxn];
int ans[maxn];
pair<int,int> point[maxn];
vector<pair<int,int> > G[maxn];
void pro(int u,int x){
    dis[u]=x;
    int len=G[u].size();
    for(int i=0;i<len;i++){
        int v=G[u][i].first;
        if(vis[v])continue;
        vis[v]=true;
        ans[G[u][i].second]=true;
    }
}
int main(){

    int T,u,v;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&N,&M);
        for(int i=0;i<=N;i++){
            G[i].clear();
        }
        for(int i=1;i<=M;i++){
            scanf("%d%d",&u,&v);
            point[i]=make_pair(u,v);
            G[u].push_back(make_pair(v,i));
        }
        memset(ans,0,sizeof(ans));
        memset(vis,0,sizeof(vis));
        int s=1,t=N;
        vis[1]=true;
        dis[1]=0;
        int x=1,cur=0;
        while(s<=t){
            if(cur&1){
                if(vis[t]){
                    pro(t--,x++);
                }
            }else {
                if(vis[s]){
                    pro(s++,x++);
                }
            }
            cur^=1;
        }
        for(int i=1;i<=M;i++){
            if(ans[i]){
                printf("%d\n",abs(dis[point[i].first]-dis[point[i].second]));
            } else {
                printf("%d\n",N);
            }
        }
    }
    return 0;
}

Cover

Problem Description

You have an n∗n matrix.Every grid has a color.Now there are two types of operating:
L x y: for(int i=1;i<=n;i++)color[i][x]=y;
H x y:for(int i=1;i<=n;i++)color[x][i]=y;
Now give you the initial matrix and the goal matrix.There are m operatings.Put in order to arrange operatings,so that the initial matrix will be the goal matrix after doing these operatings

It’s guaranteed that there exists solution.

Input

There are multiple test cases,first line has an integer T
For each case:
First line has two integer n,m
Then n lines,every line has n integers,describe the initial matrix
Then n lines,every line has n integers,describe the goal matrix
Then m lines,every line describe an operating

1≤color[i][j]≤n
T=5
1≤n≤100
1≤m≤500

Output

For each case,print a line include m integers.The i-th integer x show that the rank of x-th operating is i

Sample Input

1
3 5
2 2 1
2 3 3
2 1 3
3 3 3
3 3 3
3 3 3
H 2 3
L 2 2
H 3 3
H 1 3
L 2 3

Sample Output

5 2 4 3 1
这里写图片描述

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=110;
const int maxm=1010;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;

int N,M;
int a[maxn][maxn];
int ans[510];
struct OP{
    char op[5];
    int x,y;
}ope[510];
void solve(){
    int cnt=0,j;
    while(cnt<M){
        for(int i=1;i<=M;i++){
            if(!ope[i].x)continue;
            int k=ope[i].x;
            if(ope[i].op[0]=='L'){
                for(j=1;j<=N;j++){
                    if(a[j][k]&&a[j][k]!=ope[i].y){
                        break;
                    }
                }
                if(j>N){
                    ans[++cnt]=i;
                    ope[i].x=0;
                    for(j=1;j<=N;j++){
                        a[j][k]=0;
                    }
                }

            } else {
                for(j=1;j<=N;j++){
                    if(a[k][j]&&a[k][j]!=ope[i].y){
                        break;
                    }
                }
                if(j>N){
                    ans[++cnt]=i;
                    ope[i].x=0;
                    for(j=1;j<=N;j++){
                        a[k][j]=0;
                    }
                }
            }
        }
    }
    for(int i=M;i>=1;i--){
        printf("%d ",ans[i]);
    }
    printf("\n");
}
int main(){

    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&N,&M);
        for(int i=1;i<=N;i++){
            for(int j=1;j<=N;j++){
                scanf("%d",&a[i][j]);
            }
        }
        for(int i=1;i<=N;i++){
            for(int j=1;j<=N;j++){
                scanf("%d",&a[i][j]);
            }
        }
        for(int i=1;i<=M;i++){
            scanf("%s%d%d",ope[i].op,&ope[i].x,&ope[i].y);
        }
        solve();
    }
    return 0;
}

也可以用拓扑排序做
如果不在拓扑图里面的一定是废操作,然后所以得废操作可以全部放到最前面,而且对后面的操作没有任何影响。如果废操作会染到后面正确操作影响不到的点,那么这个染色的过程一定是不可行的(题意说一定可行)于是你就判断,对于每一个点,我们将 可以影响到它的操作但是不能染到正确颜色的操作 往 可以影响到它的操作并且染到正确颜色的操作 连边。对于连边,只有L和H之间连边。然后所有没有连边的点,就是废操作,直接输出。然后再跑拓扑排序
不过拓扑排序好像有种样例跑不出来
3
1 1 1
2 2 2
3 3 3
1 1 1
2 2 2
3 3 3
H 1 1
H 1 3

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;

const int maxn=110;
const int maxm=1010;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;
int N,M;
int type[510];
struct Node{
    int id,y;
    Node(){}
    Node(int _id,int _y):id(_id),y(_y){}
};
vector<Node> row[maxn],col[maxn];
int a[maxn][maxn],b[maxn][maxn];
int tmp1[510],tmp2[510];
int in[510];
int vis[510][510];
void solve(){
    int cnt1=0,cnt2=0;
    memset(in,0,sizeof(in));
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=N;i++){
        for(int j=1;j<=N;j++){
            cnt1=0,cnt2=0;
            int len=row[i].size();
            for(int k=0;k<len;k++){
                if(row[i][k].y==b[i][j]){
                    tmp1[cnt1++]=row[i][k].id;
                } else {
                    tmp2[cnt2++]=row[i][k].id;
                }
            }
            len=col[j].size();
            for(int k=0;k<len;k++){
                if(col[j][k].y==b[i][j]){
                    tmp1[cnt1++]=col[j][k].id;
                } else {
                    tmp2[cnt2++]=col[j][k].id;
                }
            }

            for(int p=0;p<cnt2;p++){
                for(int q=0;q<cnt1;q++){
                    if(type[tmp2[p]]==type[tmp1[q]])continue;
                    if(!vis[tmp2[p]][tmp1[q]]){
                        in[tmp1[q]]++;
                        vis[tmp2[p]][tmp1[q]]=1;
                    }
                }
            }
        }
    }
}
int ans[510];
void topo(){
    queue<int> q;
    for(int i=1;i<=M;i++){
        if(in[i]==0)q.push(i);
    }
    int cnt=0;
    while(!q.empty()){
        int t=q.front();
        q.pop();
        ans[cnt++]=t;
        for(int i=1;i<=M;i++){
            if(vis[t][i]){
                if(--in[i]==0){
                    q.push(i);
                }
            }
        }
    }
    for(int i=0;i<cnt;i++){
        printf("%d",ans[i]);
        if(i==cnt-1)printf("\n");
        else printf(" ");
    }
}
int main(){

    int T;
    char op[5];
    int x,y;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&N,&M);
        for(int i=0;i<=N;i++){
            row[i].clear();
            col[i].clear();
        }
        for(int i=1;i<=N;i++){
            for(int j=1;j<=N;j++){
                scanf("%d",&a[i][j]);
            }
        }
        for(int i=1;i<=N;i++){
            for(int j=1;j<=N;j++){
                scanf("%d",&b[i][j]);
            }
        }
        for(int i=1;i<=M;i++){
            scanf("%s%d%d",op,&x,&y);
            type[i]=op[0]=='L'?0:1;
            if(op[0]=='L'){
                col[x].push_back(Node(i,y));
            } else {
                row[x].push_back(Node(i,y));
            }
        }
        solve();
        topo();
    }
    return 0;
}

Zero Escape

Problem Description

Zero Escape, is a visual novel adventure video game directed by Kotaro Uchikoshi (you may hear about ever17?) and developed by Chunsoft.

Stilwell is enjoying the first chapter of this series, and in this chapter digital root is an important factor.

This is the definition of digital root on Wikipedia:
The digital root of a non-negative integer is the single digit value obtained by an iterative process of summing digits, on each iteration using the result from the previous iteration to compute a digit sum. The process continues until a single-digit number is reached.
For example, the digital root of 65536 is 7, because 6+5+5+3+6=25 and 2+5=7.

In the game, every player has a special identifier. Maybe two players have the same identifier, but they are different players. If a group of players want to get into a door numbered X(1≤X≤9), the digital root of their identifier sum must be X.
For example, players {1,2,6} can get into the door 9, but players {2,3,3} can’t.

There is two doors, numbered A and B. Maybe A=B, but they are two different door.
And there is n players, everyone must get into one of these two doors. Some players will get into the door A, and others will get into the door B.
For example:
players are {1,2,6}, A=9, B=1
There is only one way to distribute the players: all players get into the door 9. Because there is no player to get into the door 1, the digital root limit of this door will be ignored.

Given the identifier of every player, please calculate how many kinds of methods are there, mod 258280327.

Input

The first line of the input contains a single number T, the number of test cases.
For each test case, the first line contains three integers n, A and B.
Next line contains n integers idi, describing the identifier of every player.
T100,n105,n106,1A,B,idi9

Output

For each test case, output a single integer in a single line, the number of ways that these n players can get into these two doors.

Sample Input

4
3 9 1
1 2 6
3 9 1
2 3 3
5 2 3
1 1 1 1 1
9 9 9
1 2 3 4 5 6 7 8 9

Sample Output

1
0
10
60

思路:如果能找到满足题意的解,一定满足a和b的和等于n个人的标号的和,所以我们只需要判断n个人的标号组成a的情况有多少(或者只判断b,一样),同时还要注意可以把n个人都分给a,或者都分给b,这样也是满足的。

#include<bits/stdc++.h>
using namespace std;
const int maxn=100010;
const int MOD=258280327;
int dp[maxn][10];
int N,a[maxn],A,B;

int cal(int x,int y){
    if((x+y)%9==0)return 9;
    return (x+y)%9;
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d%d",&N,&A,&B);
        memset(dp,0,sizeof(dp));
        int sum=0;
        for(int i=1;i<=N;i++){
            scanf("%d",&a[i]);
            sum=cal(sum,a[i]);
        }
        dp[0][0]=1;
        for(int i=1;i<=N;i++){
            for(int j=0;j<=9;j++){
                (dp[i][j]+=dp[i-1][j])%=MOD;
                int x=cal(j,a[i]);
                (dp[i][x]+=dp[i-1][j])%=MOD;
            }
        }
        int ans=0;
        if(cal(A,B)==sum){
            ans=dp[N][A];
            if(A==sum)ans--;
        }
        if(A==sum){
            ans++;
        }
        if(B==sum){
            ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值