您的位置:首页 > 其它

UVALive - 3490 Generator 【数学】【高斯消元】

2017-02-26 23:07 381 查看
We can generate a random string by generating a sequence of random characters and concatenating
them together. Each character is chosen independently from the first n letters in the English alphabet
with equal probability. Only capital letters are used in this problem. The generation is stopped as soon
as a specific pattern occurs in the random string.
Your task is to predict the expected length of the generated string.
Input
Standard input will contain multiple test cases. The first line of the input is a single integer T (1 ≤
T ≤ 10) which is the number of test cases. T test cases follow, each preceded by a single blank line.
Each test case consists of a single integer N (1 ≤ N ≤ 26) which is the number of letters used, and
a pattern, which is a non-empty string consisting of letters chosen from the first N upper case English
letters. The length of any pattern will not exceed 12.
Output
Results should be directed to standard output. Start each case with ‘Case #:’ on a single line, where
# is the case number starting from 1. Two consecutive cases should be separated by a single blank
line. No blank line should be produced after the last test case.
For each test case, print the expected length of the generated random string.
Sample Input
5
2 A
2 ABA
3 AAAAA
26 ACMICPC
26 ZJUZJU
Sample Output
Case 1:
2

Case 2:
10

Case 3:
363

Case 4:
8031810176

Case 5:
308933352


设m为模式串p的长度,n为可以使用的字母个数

设x[i]=构造的随机串结尾最多能配对i个字符,需要再随机加多少个字符才能完全匹配|0<=i<m

设c[i][j]=随机串结尾最多能匹配i个字符,在结尾加上第j个字母,最多能匹配几个字符

x[i]=1+(1/n)∗∑n−1j=0x[c[i][j]]

此处模式串p很短直接暴力即可,得到m条方程,高斯消元求解

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
#include<queue>
#include<sstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>

const int inf=1e9+7;
const int N=26+3;
int c

;

bool endWith(const string&str,const string&p,const int len){
int i=str.size()-1;
int j=len-1;
while(j>=0){
if(str[i]!=p[j]){
return false;
}
--j,--i;
}
return true;
}

void init(const string&p,const int n){
for(int i=0;i<p.size();++i){
for(int j=0;j<n;++j){
string str(p.begin(),p.begin()+i);
str+='A'+j;
for(int len=min(p.size(),str.size());len>=0;--len){
if(endWith(str,p,len)){
c[i][j]=len;
break;
}
}

}
}
}

struct Num{
ll fz,fm;
Num(){
fz=0,fm=1;
}

Num(ll a,ll b):fz(a),fm(b){}

Num operator*(const Num&x){
Num ans;
ans.fz=fz*x.fz;
ans.fm=fm*x.fm;
ans.relax();
return ans;
}

Num operator/(const Num&x){
Num ans;
ans.fz=fz*x.fm;
ans.fm=fm*x.fz;
ans.relax();
return ans;
}

Num operator-(const Num&x){
Num ans;
ans.fz=fz*x.fm-fm*x.fz;
ans.fm=fm*x.fm;
ans.relax();
return ans;
}

Num&operator-=(const Num&x){
*this=*this-x;
return *this;
}

void relax(){
ll d=gcd(fz,fm);
fz/=d;
fm/=d;
}

ll gcd(ll a,ll b){
return b?gcd(b,a%b):a;
}
};

Num matrix

;//保存增广矩阵
//int copyMatrix

;//增广矩阵的副本
bool isFree
;
Num ans
;

int Gauss(Num a[]
,const int&m,const int&n,bool isFree[],Num ans[]){//m:变元个数 n:方程个数
int res=0,r=0;//res为自由变元个数 r为增广矩阵的秩
fill(isFree,isFree+n,false);
for(int i=0;i<m;++i){//处理第i个变元
for(int j=r;j<n;++j)//找到第i个变元系数不为0的方程 并放到第r行
if(a[j][i].fz){
for(int k=i;k<=m;++k)
swap(a[j][k],a[r][k]);
break;
}
if(a[r][i].fz==0){//第i个变元没有系数不为0的 这变元是自由变元
++res;
isFree[i]=true;
continue;
}
for(int j=0;j<n;++j)//消去其他方程的i变元
if((j!=r)&&(a[j][i].fz!=0)){
Num t=a[j][i]/a[r][i];
for(int k=i;k<=m;++k)
a[j][k]-=t*a[r][k];
}
++r;//矩阵的秩+1
}

for(int i=0,j=0;i<n;++i){//求解
if(isFree[i]){
continue;
}
ans[i]=a[j][m]/a[j][i];
++j;
}

//矩阵的秩下面的方程 系数都为0 0*x1+0*x2+...+0*xm恒等于0
for(int i=r;i<n;++i)//如果矩阵形如(0,0,0...0,a) a!=0 无解
if(a[i][m].fz)//判断是否无解
return -1;
return res;//返回自由变元个数
}

void initMatrix(const int n,int m){//n:方程变量数 m字母数量
for(int i=0;i<n;++i){
fill(matrix[i],matrix[i]+n+2,Num());
}
for(int i=0;i<n;++i){
matrix[i][i]=Num(m,1);
matrix[i]
=Num(m,1);
for(int j=0;j<m;++j){
if(c[i][j]!=n){
matrix[i][c[i][j]]-=Num(1,1);
}
}
}
}

int main()
{
//freopen("/home/lu/Documents/r.txt","r",stdin);
//freopen("/home/lu/Documents/w.txt","w",stdout);
int T;
scanf("%d",&T);
for(int t=1;t<=T;++t){
printf("Case %d:\n",t);
int n;
scanf("%d",&n);
string p;
cin>>p;
init(p,n);//初始化c

initMatrix(p.size(),n);
Gauss(matrix,p.size(),p.size(),isFree,ans);

printf("%lld\n",ans[0].fz/ans[0].fm);

if(t!=T){
putchar('\n');
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息