12.7 Test

目錄

  • 2018.12.7 Test
    • A 序列sequence(迭代加深搜索)
    • B 轟炸bomb(Tarjan DP)
    • C 字符串string(AC自動機 狀壓DP)
    • 考試代碼
      • A
      • C

2018.12.7 Test

題目為2018.1.4雅禮集訓。

時間:4.5h
期望得分:0+100+10
實際得分:0+100+10

A 序列sequence(迭代加深搜索)

顯然可以每次將最大的數轉到第一位,再轉到對應的位,所以答案不會超過\(2n-2\)
這其實挺小的,考慮爆搜迭代加深。

注意到每次反轉最多只會影響一對數的連續關系(反轉位置\(p\)\(p+1\)),所以借此可以求出當前至少還需多少步。利用這個剪枝就可以過了。。

復雜度\(O(能過)\)

#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define gc() getchar()
typedef long long LL;
const int N=27;int n,lim,OK,A[N];inline int read()
{int now=0;register char c=gc();for(;!isdigit(c);c=gc());for(;isdigit(c);now=now*10+c-'0',c=gc());return now;
}
void DFS(int step,int need)
{if(OK||step+need>lim) return;int p=n;while(A[p]==p) --p;if(!p) {OK=1; return;}
//  for(int p=2; p<=n; ++p)//這種玄學題還是倒著吧...?for(; p>=2; --p){int t=(p<n && std::abs(A[p]-A[p+1])==1)-(p<n && std::abs(A[1]-A[p+1])==1);std::reverse(A+1,A+1+p);DFS(step+1,need+t);std::reverse(A+1,A+1+p);if(OK) break;}
}int main()
{freopen("sequence.in","r",stdin);freopen("sequence.out","w",stdout);A[0]=-233;for(int T=read(); T--; ){n=read(); int t=0;for(int i=1; i<=n; ++i) A[i]=read();for(int i=1; i<n; ++i) if(std::abs(A[i]-A[i+1])!=1) ++t;for(OK=0,lim=t; ; ++lim)if(DFS(0,t),OK) break;printf("%d\n",lim);}return 0;
}

B 轟炸bomb(Tarjan DP)

縮點,將每個連通分量縮點后的權值設成連通分量大小,那么就是求每個連通塊的最長鏈了。簡單DP一下。

另外可以\(O(1)\)將連通分量中的點的出邊接到根節點(連通分量代表點)的邊表后面去,訪問那些點就直接訪問根節點好了。這樣無需新建一張圖。

#include <cstdio>
#include <cctype>
#include <algorithm>
//#define gc() getchar()
#define MAXIN 300000
#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
typedef long long LL;
const int N=1e6+5;int Ans,Enum,ed[N],H[N],nxt[N<<1],to[N<<1],dfn[N],low[N],top,sk[N],bel[N],val[N],f[N];
bool vis[N],ins[N];
char IN[MAXIN],*SS=IN,*TT=IN;inline int read()
{int now=0;register char c=gc();for(;!isdigit(c);c=gc());for(;isdigit(c);now=now*10+c-'0',c=gc());return now;
}
inline void AE(int v,int u)
{if(!H[u]) ed[u]=Enum+1;to[++Enum]=v, nxt[Enum]=H[u], H[u]=Enum;
}
void Tarjan(int x)
{static int Index=0;low[x]=dfn[x]=++Index, sk[++top]=x, ins[x]=1;for(int i=H[x],v; i; i=nxt[i])if(!dfn[v=to[i]]) Tarjan(v), low[x]=std::min(low[x],low[v]);else if(ins[v]) low[x]=std::min(low[x],dfn[v]);if(dfn[x]==low[x]){do{int tmp=sk[top--];++val[x], bel[tmp]=x;if(x!=tmp) nxt[ed[x]]=H[tmp], ed[x]=ed[tmp];//, H[tmp]=0;ins[tmp]=0;}while(sk[top+1]!=x);}
}
void DFS(int x)
{int mx=0; vis[x]=1;for(int i=H[x],v; i; i=nxt[i])if((v=bel[to[i]])!=x){if(!vis[v]) DFS(v);mx=std::max(mx,f[v]);}f[x]=val[x]+mx;Ans=std::max(Ans,f[x]);
}int main()
{freopen("bomb.in","r",stdin);freopen("bomb.out","w",stdout);int n=read(),m=read();for(int i=1; i<=m; ++i) AE(read(),read());for(int i=1; i<=n; ++i) bel[i]=i;for(int i=1; i<=n; ++i)if(!dfn[i]) Tarjan(i);for(int i=1; i<=n; ++i)if(!vis[i]&&bel[i]==i) DFS(i);printf("%d\n",Ans);return 0;
}

C 字符串string(AC自動機 狀壓DP)

1143196-20181210063941390-309797461.png
原題:HDU 6086

考慮沒有反回文這一條件,那么將\(n\)個字符串都插入AC自動機,就可以在上面狀壓DP了(狀壓匹配了哪幾個字符串)。
具體就是,令\(f[i][j][k]\)表示當前為第\(i\)位,當前在AC自動機上的節點\(j\),匹配字符串狀態為\(k\),的方案數。轉移時,枚舉節點\(u\),然后枚舉下一步放哪個字符\(c\),然后會跳到一個節點\(v=son[u][c]\)。那么\(f[i][u][k]\)就可以轉移到\(f[i+1][v][k|s_v]\),其中\(s_v\)為在\(v\)節點匹配的字符串集合。
(這樣當然對啊,雖然只是對\(n\)個串建AC自動機,但每次填一個字符一定會轉移到某個節點且仍保持某種匹配狀態)
最后答案就是\(\sum_{i=1}^{tot}f[m][i][2^n-1]\)\(tot\)是AC自動機總結點數。

考慮反回文的情況。那么每個串在長\(2m\)的串中出現有四種情況:在前一半出現,在后一半出現,跨越中點且在前一半的部分多,跨越中點且在后一半的部分多。

對于第二種情況,把反串(reverse后再01取反)插入到AC自動機,同樣狀壓DP就好了。

對于第三種情況,我們需要判斷每個串\(s\)的每個長度至少為\(|s|\)一半的前綴\(s[1...i]\)判斷(注意\(i\)的范圍!),反轉它后是否能對應\(s[i+1...|s|]\),也就是它是否可以跨越中點。
對每個節點再狀壓一個狀態\(s'_u\),表示以該節點作為中間點(也就是前一半串的結束點)能匹配的字符串集合。最后計算答案時\(s_u\)\(s'_u\)取個并再判斷是否等于\(2^n-1\)即可。
如果該串在當前匹配節點\(u\)處可以跨越中點,就加入到\(s'_u\)中去。

對于第四種情況,對反串同情況三一樣處理即可。

//15MS  1344K
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define gc() getchar()
#define mod 998244353
#define Add(x,v) (x+=v)>=mod&&(x-=mod)
typedef long long LL;
const int N=6*20*2+7;//6*100*2+7;struct AC_Automaton
{int tot,son[N][2],s1[N],s2[N],fail[N],q[N],f[2][N][(1<<6)+2];void Clear(){tot=0, memset(son,0,sizeof son), memset(fail,0,sizeof fail);memset(s1,0,sizeof s1), memset(s2,0,sizeof s2), memset(f,0,sizeof f);}inline bool Check(char *s,int p,int l){for(int i=p+1; i<l; ++i) if(/*p-i+p+1<0||*/s[i]==s[p-i+p+1]) return 0;return 1;}void Insert(char *s,int l,int id){int x=0;for(int i=0,c,mid=l-1>>1; i<l; ++i)//mid=(l-1)/2 !{if(!son[x][c=s[i]-48]) son[x][c]=++tot;x=son[x][c];if(i>=mid && Check(s,i,l)) s2[x]|=id;}s1[x]|=id;}void Build(){int h=0,t=0;if(son[0][0]) fail[son[0][0]]=0, q[t++]=son[0][0];if(son[0][1]) fail[son[0][1]]=0, q[t++]=son[0][1];while(h<t){int x=q[h++];s1[x]|=s1[fail[x]], s2[x]|=s2[fail[x]];for(int i=0; i<2; ++i)if(son[x][i]) fail[son[x][i]]=son[fail[x]][i], q[t++]=son[x][i];else son[x][i]=son[fail[x]][i];}}void Solve(int n,int m){
//      for(int i=0; i<=tot; ++i) s1[i]|=s1[fail[i]], s2[i]|=s2[fail[i]];int p=1,lim=(1<<n)-1; f[p][0][0]=1;for(int i=1; i<=m; ++i,p^=1)for(int u=0; u<=tot; ++u)for(int k=0,val; k<=lim; ++k){if(!(val=f[p][u][k])) continue;for(int l=0; l<2; ++l){int v=son[u][l],s=k|s1[v];Add(f[p^1][v][s],val);}f[p][u][k]=0;}LL ans=0;for(int u=0; u<=tot; ++u)for(int k=0; k<=lim; ++k)if((k|s2[u])==lim) ans+=f[p][u][k];printf("%d\n",(int)(ans%mod));}
}ac;inline int read()
{int now=0;register char c=gc();for(;!isdigit(c);c=gc());for(;isdigit(c);now=now*10+c-'0',c=gc());return now;
}int main()
{freopen("string.in","r",stdin);freopen("string.out","w",stdout);static char tmp[105];int n=read(),m=read();for(int i=0,l; i<n; ++i){scanf("%s",tmp), l=strlen(tmp);ac.Insert(tmp,l,1<<i);std::reverse(tmp,tmp+l);for(int j=0; j<l; ++j) tmp[j]^=1;//就算是'0''1',相鄰的一個奇數一個偶數也可以直接轉啊 ac.Insert(tmp,l,1<<i);}ac.Build(), ac.Solve(n,m);return 0;
}

考試代碼

A

自閉。

#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define gc() getchar()
#define MAXIN 300000
//#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
typedef long long LL;
const int N=27;int A[N];
char IN[MAXIN],*SS=IN,*TT=IN;inline int read()
{int now=0,f=1;register char c=gc();for(;!isdigit(c);c=='-'&&(f=-1),c=gc());for(;isdigit(c);now=now*10+c-'0',c=gc());return now*f;
}
int Solve1(int n)
{static int B[N];memcpy(B,A,sizeof A);int now=n,ans=0,lim=n; B[0]=-233;while(now!=1){while(B[lim]==now) --lim, --now;if(!lim) break;if(B[1]==now) std::reverse(B+1,B+1+lim), --lim, --now, ++ans;else{int p=1;for(int i=1; i<=lim; ++i) if(B[i]==now) {p=i; break;}std::reverse(B+1,B+1+p), ++ans;}}return ans;
}
bool De=1;
int Solve2(const int n)
{static int f[N][N][N][2];//0:up 1:downmemset(f,0x3f,sizeof f);for(int i=1; i<=n; ++i){f[i][i][A[i]][0]=f[i][i][A[i]][1]=0;for(int j=i+1; j<=n; ++j)if(A[j]==A[i]+j-i) f[i][j][A[i]][0]=0, f[i][j][A[i]][1]=0;else break;for(int j=i+1; j<=n; ++j)if(A[j]==A[i]-j+i) f[i][j][A[j]][1]=0, f[i][j][A[j]][0]=0;else break;}for(int l=1; l<n; ++l){De && printf("\nl:%d\n",l);for(int i=1; i+l<=n; ++i){int j=i+l;for(int x=1; x<=n; ++x){for(int k=i; k<j; ++k){if(x+k+1-i<=n) f[i][j][x][0]=std::min(f[i][j][x][0],f[i][k][x][0]+f[k+1][j][x+k+1-i][0]);if(x+j-k<=n) f[i][j][x][1]=std::min(f[i][j][x][1],f[i][k][x+j-k][1]+f[k+1][j][x][1]);if(x+k+1-i<=n) De && printf("f[%d][%d][%d][%d]=%d\n",i,j,x,0,f[i][j][x][0]);if(x+j-k<=n) De && printf("f[%d][%d][%d][%d]=%d\n",i,j,x,1,f[i][j][x][1]);}}}for(int x=1; x<=n; ++x){f[1][l+1][x][0]=std::min(f[1][l+1][x][0],f[1][l+1][x][1]+1),f[1][l+1][x][1]=std::min(f[1][l+1][x][1],f[1][l+1][x][0]+1);De && printf("f[%d][%d][%d][%d]=%d\n",1,l+1,x,0,f[1][l+1][x][0]);De && printf("f[%d][%d][%d][%d]=%d\n",1,l+1,x,1,f[1][l+1][x][1]);}}return std::min(f[1][n][1][0],f[1][n][1][1]+1);
}int main()
{freopen("sequence.in","r",stdin);freopen("sequence.out","w",stdout);for(int T=read(); T--; ){int n=read();for(int i=1; i<=n; ++i) A[i]=read();int ans=Solve1(n);
//      De=1, printf("ans1:%d ans2:%d\n",ans,Solve2(n));De=0;ans=std::min(ans,Solve2(n));printf("%d\n",ans);}return 0;
}

C

#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define gc() getchar()
#define mod 998244353
typedef long long LL;
const int N=1005;int len[8],s[8][105];inline int read()
{int now=0;register char c=gc();for(;!isdigit(c);c=gc());for(;isdigit(c);now=now*10+c-'0',c=gc());return now;
}
namespace Subtask1
{int n,m,L,mid,Ans,bit[N];void Recover(int m,int L){for(int i=1; i<=m; ++i) bit[L-i+1]=bit[i]^1;}bool Check(int *s,int l){for(int i=1; i+l-1<=L; ++i)for(int j=1; ; ++j)if(j<=l && s[j]!=bit[i+j-1]) break;else if(j==l) return 1;
//      puts("Failed:"); for(int i=1; i<=l; ++i) printf("%d ",s[i]); puts("");return 0;}void DFS(int x){if(x>m){Recover(m,L);
//          puts("Now:"); for(int i=1; i<=L; ++i) printf("%d ",bit[i]); puts("");for(int i=1; i<=n; ++i)if(!Check(s[i],len[i])) return ;++Ans;return;}bit[x]=0, DFS(x+1), bit[x]=1, DFS(x+1);}void Main(int n,int m){Subtask1::n=n, Subtask1::m=m, L=m<<1;DFS(1), printf("%d\n",Ans);}
}int main()
{freopen("string.in","r",stdin);freopen("string.out","w",stdout);static char tmp[105];int n=read(),m=read();for(int i=1; i<=n; ++i){scanf("%s",tmp+1), len[i]=strlen(tmp+1);for(int j=1; j<=len[i]; ++j) s[i][j]=tmp[j]-'0';}if(m<=17) return Subtask1::Main(n,m),0;return 0;
}

轉載于:https://www.cnblogs.com/SovietPower/p/10093811.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/278718.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/278718.shtml
英文地址,請注明出處:http://en.pswp.cn/news/278718.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

word文檔中統計總頁數_如何在Google文檔中查找頁數和字數統計

word文檔中統計總頁數Whether you’ve been given an assignment with a strict limit or you just like knowing how many words you’ve written, Google Docs has your back. Here’s how to see exactly how many words or pages you’ve typed in your document. 無論您是…

vue 入門notes

文章目錄vue一、js基礎二、封裝緩存三、組件1、組件創建、引入、掛載、使用2、組件間的傳值- 父組件主動獲取子組件的數據和方法&#xff08;子組件給父組件傳值&#xff09;&#xff1a;- 子組件主動獲取父組件的數據和方法&#xff08;父組件給子組件傳值&#xff09;&#x…

spring集成 JedisCluster 連接 redis3.0 集群

2019獨角獸企業重金招聘Python工程師標準>>> spring集成 JedisCluster 連接 redis3.0 集群 博客分類&#xff1a; 緩存 spring 客戶端采用最新的jedis 2.7 1. maven依賴&#xff1a; <dependency> <groupId>redis.clients</groupId> <artifact…

html-盒子模型及pading和margin相關

margin: <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>Title</title><style>* {margin: 0;padding: 0;}/*margin 外邊距 元素與其他元素的距離&#xff08;邊框以外的距離&#xff09;一…

火狐瀏覽器復制網頁文字_從Firefox中的網頁鏈接的多種“復制”格式中選擇

火狐瀏覽器復制網頁文字Tired of having to copy, paste, and then format links for use in your blogs, e-mails, or documents? Then see how easy it is to choose a click-and-go format that will save you a lot of time and effort with the CoLT extension for Firef…

vscode配置、使用git

文章目錄一、下載、配置git二、vscode配置并使用git三、記住密碼一、下載、配置git 1、git-win-x64點擊下載后安裝直接安裝&#xff08;建議復制鏈接用迅雷等下載器下載&#xff0c;瀏覽器太慢&#xff0c;記住安裝位置&#xff09;。 2、配置git環境變量&#xff1a; 右鍵 此…

BTrace功能

2019獨角獸企業重金招聘Python工程師標準>>> BTrace功能 一、背景 在生產環境中可能經常遇到各種問題&#xff0c;定位問題需要獲取程序運行時的數據信息&#xff0c;如方法參數、返回值、全局變量、堆棧信息等。為了獲取這些數據信息&#xff0c;我們可以…

.NET(c#) 移動APP開發平臺 - Smobiler(1)

原文&#xff1a;https://www.cnblogs.com/oudi/p/8288617.html 如果說基于.net的移動開發平臺&#xff0c;目前比較流行的可能是xamarin了&#xff0c;不過除了這個&#xff0c;還有一個比xamarin更好用的國內的.net移動開發平臺&#xff0c;smobiler&#xff0c;不用學習另外…

如何在Vizio電視上禁用運動平滑

Vizio維齊奧New Vizio TVs use motion smoothing to make the content you watch appear smoother. This looks good for some content, like sports, but can ruin the feel of movies and TV shows. 新的Vizio電視使用運動平滑來使您觀看的內容顯得更平滑。 這對于某些內容(例…

無服務器架構 - 從使用場景分析其6大特性

2019獨角獸企業重金招聘Python工程師標準>>> 無服務器架構 - 從使用場景分析其6大特性 博客分類&#xff1a; 架構 首先我應該提到&#xff0c;“無服務器”技術肯定有服務器涉及。 我只是使用這個術語來描述這種方法和技術&#xff0c;它將任務處理和調度抽象為與…

ES6實用方法Object.assign、defineProperty、Symbol

文章目錄1.合并對象 - Object.assign()介紹進階注意用途2.定義對象 - Object.defineProperty(obj, prop, descriptor)3.新數據類型- Symbol定義應用1.合并對象 - Object.assign() 介紹 assign方法可以將多個對象&#xff08;字典&#xff09;&#xff0c;語法&#xff1a;Obj…

Enable Authentication on MongoDB

1、Connect to the server using the mongo shell mongo mongodb://localhost:270172、Create the user administrator Change to the admin database: use admindb.createUser({user: "Admin",pwd: "Admin123",roles: [ { role: "userAdminAnyDataba…

windows驅動程序編寫_如何在Windows中回滾驅動程序

windows驅動程序編寫Updating a driver on your PC doesn’t always work out well. Sometimes, they introduce bugs or simply don’t run as well as the version they replaced. Luckily, Windows makes it easy to roll back to a previous driver in Windows 10. Here’s…

運行tomcat報Exception in thread ContainerBackgroundProcessor[StandardEngine[Catalina]]

解決方法1&#xff1a; 手動設置MaxPermSize大小&#xff0c;如果是linux系統&#xff0c;修改TOMCAT_HOME/bin/catalina.sh&#xff0c;如果是windows系統&#xff0c;修改TOMCAT_HOME/bin/catalina.bat&#xff0c; 在“echo "Using CATALINA_BASE: $CATALINA_BASE&q…

new子類會先運行父類的構造函數

發現子類構造函數運行時&#xff0c;先運行了父類的構造函數。為什么呢? 原因&#xff1a;子類的所有構造函數中的第一行&#xff0c;其實都有一條隱身的語句super(); super(): 表示父類的構造函數&#xff0c;并會調用于參數相對應的父類中的構造函數。而super():是在調用父類…

在Windows 7中的Windows Media Player 12中快速預覽歌曲

Do you ever wish you could quickly preview a song without having to play it? Today we look at a quick and easy way to do that in Windows Media Player 12. 您是否曾經希望無需播放就可以快速預覽歌曲&#xff1f; 今天&#xff0c;我們探討一種在Windows Media Play…

Vue.js中的8種組件間的通信方式;3個組件實例是前6種通信的實例,組件直接復制粘貼即可看到運行結果

文章目錄一、$children / $parent二、props / $emit三、eventBus四、ref五、provide / reject六、$attrs / $listeners七、localStorage / sessionStorage八、Vuex實例以element ui為例。例子從上往下逐漸變復雜&#xff08;后面例子沒有刪前面的無用代碼&#xff0c;有時間重新…

不可思議的混合模式 background-blend-mode

本文接前文&#xff1a;不可思議的混合模式 mix-blend-mode 。由于 mix-blend-mode 這個屬性的強大&#xff0c;很多應用場景和動效的制作不斷完善和被發掘出來&#xff0c;遂另起一文繼續介紹一些使用 mix-blend-mode 制作的酷炫動畫。 CSS3 新增了一個很有意思的屬性 -- mix-…

adb錯誤 - INSTALL_FAILED_NO_MATCHING_ABIS

#背景 換組啦&#xff0c;去了UC國際瀏覽器&#xff0c;被擁抱變化了。還在熟悉階段&#xff0c;嘗試了下adb&#xff0c;然后就碰到了這個INSTALL_FAILED_NO_MATCHING_ABIS的坑。。。 #解決方法 INSTALL_FAILED_NO_MATCHING_ABIS is when you are trying to install an app th…

如何更改從Outlook發送的電子郵件中的“答復”地址

If you’re sending an email on behalf of someone else, you might want people to reply to that person instead of you. Microsoft Outlook gives you the option to choose a different default Reply address to cover this situation. 如果您要代表其他人發送電子郵件&…