1. 程式人生 > >【Codeforces332E】Binary Key

【Codeforces332E】Binary Key

題意:

  • 給出兩個字串和和一個正整數,要求構造長度為的串,滿足以下要求:
    • 將其不斷複製到長度大於等於。
    • 對應跑這個串,如果當前位置為,則將當前對應的字元加入一個串的結尾。(開始為空串)
    • 最後得到的串與相等。
  • 輸出滿足要求的字典序最小的串。

題解:

  • 按照模的值分類,儘量讓字典序小,通過雜湊判斷是否可行即可。

程式碼:

#include <bits/stdc++.h>
#define gc getchar()
#define ll long long
#define A 2333
#define N 1000009
#define M 2009
using namespace
std; char s1[N],s2[M],ans[M],now[M]; int len1,len2,k; unsigned long long ha[N],Ha[M]; int read() { int x=1; char ch; while (ch=gc,ch<'0'||ch>'9') if (ch=='-') x=-1; int s=ch-'0'; while (ch=gc,ch>='0'&&ch<='9') s=s*10+ch-'0'; return s*x; } void check(int t) { for
(int i=len2-1;~i;i--) Ha[i]=(i+t<len2?Ha[i+t]:0)*A+s2[i]; int rest=t-1; for (int j=k-1;~j;j--) if (rest>=0&&ha[j]==Ha[rest]) { now[j]='1'; rest--; } else now[j]='0'; if (rest>=0) now[0]=0; } int main() { //ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
gets(s1); len1=strlen(s1); gets(s2); len2=strlen(s2); k=read(); for (int i=len1-1;~i;i--) ha[i]=(i+k<len1?ha[i+k]:0)*A+s1[i]; for (int i=1;i<=200;i++) { if (i*(len1/k)+max(0,i-(k-len1%k))>len2) continue; if (i*(len1/k)+min(len1%k,i)<len2) continue; check(i); if (now[0]&&(!ans[0]||strcmp(now,ans)==-1)) strcpy(ans,now); } ans[k]='\0'; if (!ans[0]) cout<<0<<endl; else cout<<ans<<endl; return 0; }

相關推薦

Codeforces332EBinary Key

題意: 給出兩個字串和和一個正整數,要求構造長度為的串,滿足以下要求: 將其不斷複製到長度大於等於。 對應跑這個串,如果當前位置為,則將當前對應的字元加入一個串的結尾。(開始為空串) 最後得到的串

LeetCodePythonBinary Tree Inorder Traversal

nod 不知道 otto div ack return integer neu else Given a binary tree, return the inorder traversal of its nodes‘ values. For example: Gi

CF662CBinary Table 按位處理

urn strong class i++ xor ios ++ limit mes 【CF662C】Binary Table 題意:給你一個$n\times m$的01網格,你可以進行任意次操作,每次操作是將一行或一列的數都取反,問你最多可以得到多少個1? $n\le

筆記Binary Extended Euclidean Algorithm

擴充套件歐幾里得演算法 給定非負整數a, b,求解向量(u1, u2, u3),使得au1 + bu2 = u3 = gcd(a, b)。 擴充套件歐幾里得演算法的除法版本 引入輔助向量(v1, v2, v3),使得av1 + bv2 = v3 程式碼如下: /// <summ

筆記Binary Euclidean Algorithm

歐幾里得演算法 u,v都是偶數時 gcd(u, v) = 2gcd(u/2, v/2) u,v只有一個偶數時,偶數u時  gcd(u, v) = gcd(u/2, v);偶數v時 gcd(u, v) = gcd(u, v/2) u,v都是奇數時,u > v時 gcd

轉載pygame.key 鍵值說明

來源 https://blog.csdn.net/stoneyyhit/article/details/52259993   pygame.key pygame.key pygame module to work with the keyboard

LeetCodeBinary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2

LeetCodeBinary Tree Inorder Traversal 二叉樹中序遍歷遞迴以及非遞迴演算法

  Total Accepted: 16494 Total Submissions: 47494 Given a binary tree, return the inorder traversal of its nodes' values. For example: Giv

leetCodeBinary Tree Level Order Traversal python實現

Binary Tree Level Order Traversal 原題連結 實現原理解析 層次遍歷即可 python程式碼實現 class Solution(object): def __init__(self

LeetCodeBinary Tree Preorder Traversal 二叉樹前序遍歷遞迴以及非遞迴演算法

  Total Accepted: 17403 Total Submissions: 50093 My Submissions Given a binary tree, return the preorder traversal of its nodes' values.

LeetCodeBinary Tree Postorder Traversal 二叉樹後序遍歷遞迴以及非遞迴演算法

Total Accepted: 15614 Total Submissions: 50928 My Submissions Given a binary tree, return the postorder traversal of its nodes' values.

題解Binary Tree Right Side View

這道leetcode最新出的題,貌似也不是很難,用廣度優先搜尋即可,參考程式碼如下。 class Solution { public: vector<int> rightSideView(TreeNode *root) { vector&

leetcode107Binary Tree Level Order Traversal II

一、問題描述 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level fr

WriteupBoston Key Party CTF 2015(部分題目)

假期試著做了一下這場美國的CTF比賽,無奈題目看了一遍都沒什麼想法,只好等比賽結束再學習了。在這裡總結一下學到的姿勢。 (以下是六道php程式碼審計題目) 1.Prudential   I don‘t think that sha1 is broken. Prove me

Lintcode069.Binary Tree Level Order Traversal

vector pub i++ pre oot order ptr values logs 題目: Given a binary tree, return the level order traversal of its nodes‘ values. (ie, from le

Java Map按照Key和Value排序

try lin tor put array ava ble nbsp key package kingtool.sort; import java.util.ArrayList; import java.util.Collections; import java.ut

LeetCode-面試算法經典-Java實現106-Construct Binary Tree from Inorder and Postorder Traversal(構造二叉樹II)

struct ons node dcl 實現 ftl rsa tor var 【106-Construct Binary Tree from Inorder and Postorder Traversal(通過中序和後序遍歷構造二叉樹)】 【Lee

606. Construct String from Binary Tree easy

new mos pen you image color emp same exce 606. Construct String from Binary Tree 【easy】 You need to construct a string consists of paren

pythonjson中字典key不可為數值型

pre size color code 不可 bsp 存在 clas 問題 遇到了一個很詭異的錯誤。寫一個字典,存入json文件。之後讀出判斷是否存在key。結果驚奇的發現,同一個key居然存在兩次。 原因:json會將數值key轉換為unicode 結論:使用json

Leetcode110. Balanced Binary Tree

out method easy fin nod gen als lan generated Question: Given a binary tree, determine if it is height-balanced. For this problem, a he