cf1537 Round #726 Div2-E1、E2【思维+找结论】

Date:2022.01.08

题意:给个字符串s,两种操作:
①去掉最后一个字符
②字符串后接上自身
求操作后字典序最小的、长度为k的串。
在这里插入图片描述
E1:1<=n,k<=50001<=n,k<=50001<=n,k<=5000
E2:1<=n,k<=5∗1051<=n,k<=5 * 10^51<=n,k<=5105

思路①:先看E1,首先能发现一个结论,即:长度为k且字典序最小的串一定是原串的某个前缀(因此此处可能先进行了操作1)进行若干次操作2得到的串取前k个字符,为什么?
反证:假设字典序最小的串在操作2的过程中进行了若干次操作1,则说明对比正常进行操作2若干次出现了某部分字典序是更小的,又因为操作1是删除了最后的字母,因此这一部分字典序更小的子串一定是原串中的某个字典序更小的前缀,那么如果我们一开始选择它去进行操作2,得到的字典序一定会更小。举个例子:
假设要得到长度为16的串,首先取前缀:dbcde,首先进行操作2翻一倍得到dbcdedbcde,此时我们再进行操作1删除末尾两个字母得到dbcdedbc;再翻倍得到dbcdedbcdbcdedbc,而原串直接操作2则为dbcdedbcdedbcded,显然第一个串更短,那么此时我们发现对比原前缀dbcde,我们发现操作过程中有个更小的前缀:dbc,那我们用dbc翻倍则字典序会更小。原理即如此,因此结论成立。
由此,思路①即为枚举所有前缀并翻倍,找出其中字典序最小的,再取前k个字符输出。
代码如下:

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 5050;
typedef long long LL;
LL n,m,t;
char s[N];
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>n>>m;
    LL ans=(m+n-1)/n;
    string minp="",p="";
    for(int i=1;i<=n;i++)
    {cin>>s[i];p+=s[i];}
    for(int i=1;i<=ans;i++) minp+=p;
    string ss="";
    for(int i=1;i<=n;i++)
    {
        LL x=(m+i-1)/i;ss+=s[i];
        string pp="";
        for(int j=1;j<=x;j++) pp+=ss;
        if(minp>pp) minp=pp;
    }
    for(int i=0;i<m;i++) cout<<minp[i];
    return 0;
}

思路②:再看E2,首先有了E1的结论我们则只需要考虑前缀选哪个,因此我们从前到后枚举i,看i+1(即s[0] ~ s[i])长度的前缀是否比当前最优前缀长度p(即s[0] ~ s[p-1])字典序更小。初始p==1,假设我们当前枚举的前缀长度为i+1,那么与最优解p有以下三种情况:
①s[i] > s[i%p]:对比于i及长度更长的串作为前缀,p是更优的,直接跳出即可。
②s[i] == s[i%p]:不管。
③s[i] < s[i%p]:用i+1长度的前缀比用p长度的前缀更优,因此更新p=i+1。
代码如下:

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 5e5+10;
typedef long long LL;
LL n,m,t;
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>n>>m;
    string s;cin>>s;
    int p=1,i=0,n=s.length();
    for(int i=0;i<n;i++)
    {
        if(s[i]>s[i%p]) break;
        if(s[i]<s[i%p]) p=i+1;
    }
    for(int i=0;i<m;i++) cout<<s[i%p];
    return 0;
}
<template> <div class="editor-layout"> <!-- 固定头部 --> <header class="app-header"> <button class="back-btn" @click="handleBack"> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <path d="M19 12H5M12 19l-7-7 7-7"/> </svg> </button> <h1 class="app-title">AI笔记编辑器</h1> <div class="header-right"> <button class="history-btn" @click="toggleHistory"> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <circle cx="12" cy="12" r="10"></circle> <polyline points="12 6 12 12 16 14"></polyline> </svg> <span>历史记录</span> </button> <div class="user-avatar" @click="goToProfile"> <div class="avatar-placeholder"> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"></path> <circle cx="12" cy="7" r="4"></circle> </svg> </div> </div> </div> </header> <!-- 固定工具栏 --> <div class="fixed-toolbar"> <Toolbar :editor="editorRef" :defaultConfig="toolbarConfig" /> </div> <!-- 编辑器区域 --> <div class="editor-container"> <Editor v-model="valueHtml" :defaultConfig="editorConfig" @onChange="handleChange" @onCreated="handleCreated" @onDestroyed="handleDestroyed" @onFocus="handleFocus" @onBlur="handleBlur" @customAlert="customAlert" @customPaste="customPaste" /> </div> <!-- 历史记录侧边栏 --> <div class="history-sidebar" :class="{ active: showHistory }"> <div class="sidebar-header"> <h2>历史记录</h2> <button class="close-btn" @click="toggleHistory"> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <line x1="18" y1="6" x2="6" y2="18"></line> <line x1="6" y1="6" x2="18" y2="18"></line> </svg> </button> </div> <div class="history-list"> <div v-for="(item, index) in historyItems" :key="index" class="history-item"> <div class="history-title">{{ item.title }}</div> <div class="history-date">{{ item.date }}</div> </div> </div> </div> <!-- 历史记录遮罩 --> <div v-if="showHistory" class="sidebar-mask" @click="toggleHistory"></div> </div> </template> <script setup> import { onBeforeUnmount, ref, shallowRef } from &#39;vue&#39; import { Editor, Toolbar } from &#39;@wangeditor/editor-for-vue&#39; import &#39;@wangeditor/editor/dist/css/style.css&#39; // 编辑器实例 const editorRef = shallowRef() // 内容 HTML const valueHtml = ref(&#39;<h1>欢迎使用AI笔记编辑器</h1><p>这是一个功能强大的富文本编辑器,支持多种格式和功能。</p><p>尝试使用工具栏上的功能来编辑内容!</p>&#39;) // 编辑器配置 const editorConfig = { placeholder: &#39;请输入内容...&#39;, MENU_CONF: { insertImage: { checkImage(src) { if (src.indexOf("http") !== 0) { return "图片网址必须以 http/https 开头"; } return true; }, }, } } // 工具栏配置 const toolbarConfig = { toolbarKeys: [ &#39;headerSelect&#39;, &#39;bold&#39;, &#39;italic&#39;, &#39;underline&#39;, &#39;through&#39;, &#39;color&#39;, &#39;bgColor&#39;, &#39;fontSize&#39;, &#39;fontFamily&#39;, &#39;lineHeight&#39;, &#39;bulletedList&#39;, &#39;numberedList&#39;, &#39;todo&#39;, &#39;justifyLeft&#39;, &#39;justifyRight&#39;, &#39;justifyCenter&#39;, &#39;insertLink&#39;, &#39;insertImage&#39;, &#39;insertTable&#39;, &#39;codeBlock&#39;, &#39;blockquote&#39;, &#39;divider&#39;, &#39;emotion&#39;, &#39;undo&#39;, &#39;redo&#39; ] } // 历史记录相关状态 const showHistory = ref(false) const historyItems = ref([ { title: &#39;AI生成的学习笔记&#39;, date: &#39;2023-10-15 14:30&#39; }, { title: &#39;项目会议记录&#39;, date: &#39;2023-10-14 09:45&#39; }, { title: &#39;技术方案设计&#39;, date: &#39;2023-10-12 16:20&#39; }, { title: &#39;读书笔记 - 人工智能导论&#39;, date: &#39;2023-10-10 11:15&#39; }, { title: &#39;周计划安排&#39;, date: &#39;2023-10-08 08:30&#39; } ]) // 编辑器回调函数 const handleCreated = (editor) => { editorRef.value = editor console.log("编辑器已创建", editor) } const handleChange = (editor) => { console.log("内容变化:", editor.children) } const handleDestroyed = (editor) => { console.log(&#39;编辑器已销毁&#39;, editor) } const handleFocus = (editor) => { console.log(&#39;编辑器获得焦点&#39;, editor) } const handleBlur = (editor) => { console.log(&#39;编辑器失去焦点&#39;, editor) } const customAlert = (info, type) => { alert(`【系统提示】${type} - ${info}`) } const customPaste = (editor, event, callback) => { console.log(&#39;粘贴事件&#39;, event) callback(true) // 继续默认的粘贴行为 } // 及时销毁编辑器 onBeforeUnmount(() => { const editor = editorRef.value if (editor == null) return editor.destroy() }) // 头部按钮功能 const handleBack = () => { alert(&#39;返回操作&#39;) } const toggleHistory = () => { showHistory.value = !showHistory.value } const goToProfile = () => { alert(&#39;跳转到个人用户管理界面&#39;) } </script> <style> /* 基础样式重置 */ * { margin: 0; padding: 0; box-sizing: border-box; font-family: &#39;Segoe UI&#39;, Tahoma, Geneva, Verdana, sans-serif; } .editor-layout { position: relative; height: 100vh; overflow: hidden; background: linear-gradient(135deg, #f5f7fa 0%, #e4edf5 100%); } /* 固定头部样式 */ .app-header { position: fixed; top: 0; left: 0; right: 0; height: 60px; display: flex; align-items: center; padding: 0 20px; background: #ffffff; box-shadow: 0 2px 15px rgba(0, 0, 0, 0.1); z-index: 1000; transition: all 0.3s ease; } .back-btn { width: 40px; height: 40px; border: none; background: none; cursor: pointer; border-radius: 50%; display: flex; align-items: center; justify-content: center; transition: all 0.2s ease; } .back-btn:hover { background: #f0f5ff; transform: translateX(-2px); } .back-btn svg { width: 20px; height: 20px; color: #4a6cf7; } .app-title { flex: 1; text-align: center; font-size: 1.4rem; font-weight: 600; color: #1a1a1a; letter-spacing: 0.5px; } .header-right { display: flex; align-items: center; gap: 15px; } .history-btn { display: flex; align-items: center; gap: 6px; padding: 8px 15px; background: #f0f5ff; border: none; border-radius: 20px; color: #4a6cf7; font-weight: 500; font-size: 0.9rem; cursor: pointer; transition: all 0.2s ease; } .history-btn:hover { background: #e1e9ff; transform: translateY(-1px); box-shadow: 0 2px 8px rgba(74, 108, 247, 0.2); } .history-btn svg { width: 18px; height: 18px; } .user-avatar { width: 40px; height: 40px; border-radius: 50%; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); display: flex; align-items: center; justify-content: center; cursor: pointer; transition: all 0.3s ease; box-shadow: 0 4px 10px rgba(118, 75, 162, 0.3); } .user-avatar:hover { transform: scale(1.05); box-shadow: 0 6px 15px rgba(118, 75, 162, 0.4); } .avatar-placeholder { width: 32px; height: 32px; border-radius: 50%; display: flex; align-items: center; justify-content: center; background: rgba(255, 255, 255, 0.2); } .avatar-placeholder svg { width: 18px; height: 18px; color: white; } /* 固定工具栏样式 */ .fixed-toolbar { position: fixed; top: 60px; /* 在头部下方 */ left: 0; right: 0; z-index: 999; background: white; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05); border-bottom: 1px solid #eaeef5; padding: 0 10px; } /* 编辑器容器样式 */ .editor-container { margin-top: 110px; /* 头部高度 + 工具栏高度 */ height: calc(100vh - 110px); overflow-y: auto; padding: 20px; background: white; border-radius: 12px; margin-left: 20px; margin-right: 20px; box-shadow: 0 5px 20px rgba(0, 0, 0, 0.05); } /* 历史记录侧边栏 */ .history-sidebar { position: fixed; top: 0; right: -400px; width: 380px; height: 100vh; background: white; z-index: 2000; box-shadow: -5px 0 25px rgba(0, 0, 0, 0.1); transition: right 0.4s cubic-bezier(0.23, 1, 0.32, 1); display: flex; flex-direction: column; } .history-sidebar.active { right: 0; } .sidebar-header { display: flex; justify-content: space-between; align-items: center; padding: 20px; border-bottom: 1px solid #eee; } .sidebar-header h2 { color: #333; font-weight: 600; } .close-btn { background: none; border: none; width: 36px; height: 36px; border-radius: 50%; display: flex; align-items: center; justify-content: center; cursor: pointer; transition: all 0.2s ease; } .close-btn:hover { background: #f5f7fa; } .close-btn svg { width: 20px; height: 20px; color: #666; } .history-list { flex: 1; overflow-y: auto; padding: 15px; } .history-item { padding: 15px; border-radius: 8px; margin-bottom: 10px; background: #f9fbfd; transition: all 0.2s ease; cursor: pointer; border-left: 3px solid #4a6cf7; } .history-item:hover { background: #edf3ff; transform: translateX(5px); } .history-title { font-weight: 500; color: #1a1a1a; margin-bottom: 5px; } .history-date { font-size: 0.85rem; color: #666; } /* 历史记录遮罩 */ .sidebar-mask { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0, 0, 0, 0.4); z-index: 1500; backdrop-filter: blur(2px); } /* 响应式设计 */ @media (max-width: 768px) { .app-header { padding: 0 10px; } .app-title { font-size: 1.1rem; } .history-btn span { display: none; } .editor-container { margin-left: 10px; margin-right: 10px; padding: 15px; } .history-sidebar { width: 85%; } } /* 滚动条美化 */ ::-webkit-scrollbar { width: 8px; } ::-webkit-scrollbar-track { background: #f1f1f1; border-radius: 4px; } ::-webkit-scrollbar-thumb { background: #c1c1c1; border-radius: 4px; } ::-webkit-scrollbar-thumb:hover { background: #a8a8a8; } </style> 我是说这个编辑器调宽一点,然后滚轮是页面的
07-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值