ZOJ - 2619 Generator 高斯消元

題目鏈接點這裏

浮點數高斯消元精度爆炸,,long double 都卡不過去。。

因爲只有一個串,,期望一定是整數,最後改成了整數高斯消元才過。。

#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f
#define FIN freopen("input.txt","r",stdin)
#define mem(x,y) memset(x,y,sizeof(x))
typedef unsigned long long ULL;
typedef long long LL;
#define fuck(x) cout<<"q"<<endl;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef pair<pair<int,int>,int> PIII;
typedef pair<int,int> PII;
const double eps=1e-10;
const int MX=111;
const int P=313;
int n,L;
struct Matrix
{
    LL w[MX][MX];
    int n;
    void init(int x)
    {
        n=x;
        for(int i=0; i<n; i++)
            for(int j=0; j<=n; j++)w[i][j]=0;

    }
};
struct AC_trie
{
    int root,cnt;
    int End[500+10],to[500+10][26],fail[500+10];//改
    int newnode()
    {
        for(int i=0; i<L; i++) to[cnt][i]=-1;//改
        End[cnt]=0;
        return cnt++;
    }
    void init()
    {
        cnt=0;
        root=newnode();
    }
    int insert(char *s)
    {
        //cout<<cnt<<endl;
        int now=root;
        for(char *ss=s; *ss; ss++)
        {
            int v=*ss-'A';//改
            if(to[now][v]==-1)to[now][v]=newnode();
            now=to[now][v];
        }
        End[now]=1;//改
        return now;
    }
    void build()
    {
        queue<int> my;
        fail[root]=root;
        for(int i = 0; i < L; i++)//改
            if(to[root][i] == -1)
                to[root][i]= root;
            else
            {
                fail[to[root][i]]=root;
                my.push(to[root][i]);
            }
        while(!my.empty())
        {
            int u=my.front();
            my.pop();
            //End[u]|=End[fail[u]];
            for(int i=0; i<L; i++)   //改
            {
                int v=to[u][i];
                if(v!=-1)
                {
                    fail[v]=to[fail[u]][i];
                    my.push(v);
                }
                else to[u][i]=to[fail[u]][i];
            }
        }
    }
    Matrix build_matrix()
    {
        Matrix ret;
        ret.init(cnt);
        for(int i=0; i<cnt; i++)
        {
            if(End[i]==0)
            {
                ret.w[i][i]=L;
                ret.w[i][cnt]=L;
                for(int j=0; j<L; j++)
                    ret.w[i][to[i][j]]+=-1;
            }
            else
            {
                ret.w[i][i]=1;
                ret.w[i][cnt]=0;
            }
        }
        return ret;
    }
} AC;
void print(Matrix a)
{
    for(int i=0; i<a.n; i++)
        for(int j=0; j<=a.n; j++)
            printf("%lld%c",a.w[i][j],j==a.n?'\n':' ');
}
char s[MX];
int  gauss(LL arr[][MX],int n)
{
    for(int i=0; i<n; i++)
    {
        int t=i;
        for(int j=i; j<n; j++) if(arr[j][i])
            {
                t=j;
                break;
            }
        if(t!=i) for(int j=i; j<=n; j++) swap(arr[i][j],arr[t][j]);
        for(int j=i+1; j<n; j++)
            if(arr[j][i])
            {
                LL g=__gcd(arr[i][i],arr[j][i]);
                LL tmp1=arr[i][i]/g,tmp2=arr[j][i]/g;
                for(int k=i; k<=n; k++)
                {
                    arr[j][k]=arr[j][k]*tmp1-arr[i][k]*tmp2;
                }
            }
    }
    for(int i=n-1; i>=0; i--)
        if(a[i][i])
        {
            arr[i][n]/=arr[i][i];
            for(int j=i-1; j>=0; j--) arr[j][n]-=arr[j][i]*arr[i][n];
        }
    return 1;//唯一解
}
int main()
{
    //freopen("output1.txt","w",stdout);
    int _;
    int cas=0;
    scanf("%d",&_);
    while(_--)
    {
        scanf("%d%s",&L,s);
        AC.init();
        AC.insert(s);
        AC.build();
        Matrix ret=AC.build_matrix();
        //print(ret);
        gauss(ret.w,ret.n);
        printf("Case %d:\n%lld\n",++cas,ret.w[0][ret.n]);
        if(_)puts("");
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章