SGU 142
题意:给你一个长度为n的串(由a,b组成),让你找出一个串不是n的子串,长度最下
收获:思维题,思路在代码里
#include<bits/stdc++.h> #define de(x) cout<<#x<<"="<<x<<endl; #define dd(x) cout<<#x<<"="<<x<<" "; #define rep(i,a,b) for(int i=a;i<(b);++i) #define repd(i,a,b) for(int i=a;i>=(b);--i) #define repp(i,a,b,t) for(int i=a;i<(b);i+=t) #define ll long long #define mt(a,b) memset(a,b,sizeof(a)) #define fi first #define se second #define inf 0x3f3f3f3f #define INF 0x3f3f3f3f3f3f3f3f #define pii pair<int,int> #define pdd pair<double,double> #define pdi pair<double,int> #define mp(u,v) make_pair(u,v) #define sz(a) (int)a.size() #define ull unsigned long long #define ll long long #define pb push_back #define PI acos(-1.0) #define qc std::ios::sync_with_stdio(false) #define db double #define all(a) a.begin(),a.end() const int mod = 1e9+7; const int maxn = 5e5+6; const double eps = 1e-6; using namespace std; bool eq(const db &a, const db &b) { return fabs(a - b) < eps; } bool ls(const db &a, const db &b) { return a + eps < b; } bool le(const db &a, const db &b) { return eq(a, b) || ls(a, b); } ll gcd(ll a,ll b) { return a==0?b:gcd(b%a,a); }; ll lcm(ll a,ll b) { return a/gcd(a,b)*b; } ll kpow(ll a,ll b) {ll res=1;a%=mod; if(b<0) return 1; for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll read(){ ll x=0,f=1;char ch=getchar(); while (ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while (ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } //inv[1]=1; //for(int i=2;i<=n;i++) inv[i]=(mod-mod/i)*inv[mod%i]%mod; //这么考虑,一个串(长度为n)拥有长度为k的子串种类最多有n-k+1种,那么题目里的n最多为5e5, //那么2的19次方就已经超过了5e5了,那么我们找的子串长度最多为19 //然后你在预处理一下就行了 char s[maxn]; bool f[(1<<19)+6][20] = {0}; int main(){ int n; scanf("%d%s",&n,s); rep(i,0,n){ int tmp = 0; // de(i) rep(j,1,20){ if(i + j > n) break; tmp = tmp * 2 + s[i+j-1] - ‘a‘; // dd(j)de(tmp) f[tmp][j] = true; } // cout<<endl; } rep(ans,1,20){ // de(ans) rep(i,0,1<<ans){ // d0e(i) if(!f[i][ans]){ printf("%d\n",ans); repd(k,ans-1,0) { if(i&(1<<k)) printf("b"); else printf("a"); } return 0; } } } return 0; }
原文地址:https://www.cnblogs.com/chinacwj/p/9061318.html
时间: 2024-09-30 10:21:21