Ural 1238 Folding 题解

题意

定义折叠、展开为:

  • 单个大写英文字母是一个折叠的串,把它展开后是它本身。
  • 如果\(S\)和\(Q\)是折叠的串,则\(SQ\)也是折叠的串。如果\(S\)展开后为\(S'\),\(Q\)展开后为\(Q'\),则\(SQ\)展开后为\(S'Q'\)。
  • 如果\(S\)是个折叠的串,则\(X(S)\)也是折叠的串,其中\(X\)是一个十进制大于\(1\)的整数,如果\(S\)展开为\(S'\),则\(X(S)\)展开后为\(S'\)重复\(X\)次。

给定一个字符串(长度小于等于\(100\)),求把它折叠后有最小长度的那个字符串。

题解

考虑记忆化搜索(DP也可以)。

定义\(f(s)\)为\(s\)字符串折叠后有最小长度的那个字符串。

边界为当\(|s|\le4\)(\(|s|\)为\(s\)的长度)时,\(f(s)=s\)。

给定\(s\),求出\(f(s)\)有以下几种方式

  • \(f(s)=s\)。

  • 设\(1<x\le|s|,|s|\%x=0\)且有字符串\(q\),\(q\)重复\(x\)次为\(s\),\(f(s)=x"("+f(q)+")"\)。

  • 设\(0<i<|s|\),\(f(s)=f(s[0..i-1])+f(s[i..|s|-1])\)。

取长度最小的值即可,不要忘了记忆化。

程序

// #pragma GCC optimize(2)
// #pragma G++ optimize(2)
// #pragma comment(linker,"/STACK:102400000,102400000") // #include <bits/stdc++.h>
#include <map>
#include <set>
#include <list>
#include <array>
#include <cfenv>
#include <cmath>
#include <ctime>
#include <deque>
#include <mutex>
#include <queue>
#include <ratio>
#include <regex>
#include <stack>
#include <tuple>
#include <atomic>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <chrono>
#include <cstdio>
#include <cwchar>
#include <future>
#include <limits>
#include <locale>
#include <memory>
#include <random>
#include <string>
#include <thread>
#include <vector>
#include <cassert>
#include <climits>
#include <clocale>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <ctgmath>
#include <cwctype>
#include <fstream>
#include <iomanip>
#include <numeric>
#include <sstream>
#include <ccomplex>
#include <cstdbool>
#include <iostream>
#include <typeinfo>
#include <valarray>
#include <algorithm>
#include <cinttypes>
#include <cstdalign>
#include <stdexcept>
#include <typeindex>
#include <functional>
#include <forward_list>
#include <system_error>
#include <unordered_map>
#include <unordered_set>
#include <scoped_allocator>
#include <condition_variable>
// #include <conio.h>
// #include <windows.h>
using namespace std; typedef long long LL;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef float fl;
typedef double ld;
typedef long double LD;
typedef pair<int,int> pii;
#if (WIN32) || (WIN64) || (__WIN32) || (__WIN64) || (_WIN32) || (_WIN64) || (WINDOWS)
#define lld "%I64d"
#define llu "%I64u"
#else
#define lld "%lld"
#define llu "%llu"
#endif
#define ui(n) ((unsigned int)(n))
#define LL(n) ((long long)(n))
#define ull(n) ((unsigned long long)(n))
#define fl(n) ((float)(n))
#define ld(n) ((double)(n))
#define LD(n) ((long double)(n))
#define char(n) ((char)(n))
#define Bool(n) ((bool)(n))
#define fixpoint(n) fixed<<setprecision(n) const int INF=1061109567;
const int NINF=-1044266559;
const LL LINF=4557430888798830399;
const ld eps=1e-15;
#define MOD (1000000007)
#define PI (3.1415926535897932384626433832795028841971) /*
#define MB_LEN_MAX 5
#define SHRT_MIN (-32768)
#define SHRT_MAX 32767
#define USHRT_MAX 0xffffU
#define INT_MIN (-2147483647 - 1)
#define INT_MAX 2147483647
#define UINT_MAX 0xffffffffU
#define LONG_MIN (-2147483647L - 1)
#define LONG_MAX 2147483647L
#define ULONG_MAX 0xffffffffUL
#define LLONG_MAX 9223372036854775807ll
#define LLONG_MIN (-9223372036854775807ll - 1)
#define ULLONG_MAX 0xffffffffffffffffull
*/ #define MP make_pair
#define MT make_tuple
#define All(a) (a).begin(),(a).end()
#define pall(a) (a).rbegin(),(a).rend()
#define log2(x) log(x)/log(2)
#define Log(x,y) log(x)/log(y)
#define SZ(a) ((int)(a).size())
#define rep(i,n) for(int i=0;i<((int)(n));i++)
#define rep1(i,n) for(int i=1;i<=((int)(n));i++)
#define repa(i,a,n) for(int i=((int)(a));i<((int)(n));i++)
#define repa1(i,a,n) for(int i=((int)(a));i<=((int)(n));i++)
#define repd(i,n) for(int i=((int)(n))-1;i>=0;i--)
#define repd1(i,n) for(int i=((int)(n));i>=1;i--)
#define repda(i,n,a) for(int i=((int)(n));i>((int)(a));i--)
#define repda1(i,n,a) for(int i=((int)(n));i>=((int)(a));i--)
#define FOR(i,a,n,step) for(int i=((int)(a));i<((int)(n));i+=((int)(step)))
#define repv(itr,v) for(__typeof((v).begin()) itr=(v).begin();itr!=(v).end();itr++)
#define repV(i,v) for(auto i:v)
#define repE(i,v) for(auto &i:v)
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x) MS(x,0)
#define MINF(x) MS(x,63)
#define MCP(x,y) memcpy(x,y,sizeof(y))
#define sqr(x) ((x)*(x))
#define UN(v) sort(All(v)),v.erase(unique(All(v)),v.end())
#define filein(x) freopen(x,"r",stdin)
#define fileout(x) freopen(x,"w",stdout)
#define fileio(x)\
freopen(x".in","r",stdin);\
freopen(x".out","w",stdout)
#define filein2(filename,name) ifstream name(filename,ios::in)
#define fileout2(filename,name) ofstream name(filename,ios::out)
#define file(filename,name) fstream name(filename,ios::in|ios::out)
#define Pause system("pause")
#define Cls system("cls")
#define fs first
#define sc second
#define PC(x) putchar(x)
#define GC(x) x=getchar()
#define Endl PC('\n')
#define SF scanf
#define PF printf inline int Read()
{
int X=0,w=0;char ch=0;while(!isdigit(ch)){w|=ch=='-';ch=getchar();}while(isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
return w?-X:X;
}
inline void Write(int x){if(x<0)putchar('-'),x=-x;if(x>9)Write(x/10);putchar(x%10+'0');} inline LL powmod(LL a,LL b){LL RES=1;a%=MOD;assert(b>=0);for(;b;b>>=1){if(b&1)RES=RES*a%MOD;a=a*a%MOD;}return RES%MOD;}
inline LL gcdll(LL a,LL b){return b?gcdll(b,a%b):a;}
const int dx[]={0,1,0,-1,1,-1,-1,1};
const int dy[]={1,0,-1,0,-1,-1,1,1};
/************************************************************Begin************************************************************/
const int maxn=110; inline string Itoa(int x)
{
string res="";
while(x)
{
res+=char(x%10+'0');
x/=10;
}
reverse(All(res));
return res;
} map<string,string> vis; inline string sol(string s)
{
if(vis.count(s)) return vis[s];
if(s.size()<=4) return vis[s]=s; string ans=s;
rep1(i,s.size()) if(i>1&&s.size()%i==0)
{
int len=s.size()/i;
string x=s.substr(0,len);
bool f=1; for(int j=0;j<s.size();j+=len) if(s.substr(j,len)!=x) f=0; if(f)
{
string res=Itoa(i)+'('+sol(x)+')';
if(ans.size()>res.size()) ans=res;
}
} rep1(i,s.size()-1)
{
string res=sol(s.substr(0,i))+sol(s.substr(i));
if(ans.size()>res.size()) ans=res;
} return vis[s]=ans;
} int main()
{
string s;
cin>>s;
cout<<sol(s); return 0;
}
/*************************************************************End**************************************************************/

Ural 1238 Folding 题解的更多相关文章

  1. Ural 1029 Ministry 题解

    目录 Ural 1029 Ministry 题解 题意 题解 程序 Ural 1029 Ministry 题解 题意 给定一个\(n\times m(1\le n \le10,1\le m \le50 ...

  2. Ural 1298 Knight 题解

    目录 Ural 1298 Knight 题解 题意 题解 程序 Ural 1298 Knight 题解 题意 给定一个\(n\times n(1\le n\le8)\)的国际象棋棋盘和一个骑士(基本上 ...

  3. 1238. Folding

    http://acm.timus.ru/problem.aspx?space=1&num=1238 DP+记忆化搜索 思路不难,关键是最优结果的储存问题,为了编写方便,直接用string储存最 ...

  4. URAL 1936 Roshambo 题解

    http://acm.timus.ru/problem.aspx?space=1&num=1936 F - Roshambo Time Limit:1000MS Memory Limit:65 ...

  5. URAL题解三

    URAL题解三 URAL 1045 题目描述:有\(n\)个机场,\(n-1\)条航线,任意两个机场有且只有一种方案联通.现有两个恐怖分子从\(m\)号机场出发,第一个人在机场安装炸弹,乘坐飞机,引爆 ...

  6. URAL题解二

    URAL题解二 URAL 1082 题目描述:输出程序的输入数据,使得程序输出"Beutiful Vasilisa" solution 一开始只看程序的核心部分,发现是求快排的比较 ...

  7. URAL题解一

    URAL题解一 URAL 1002 题目描述:一种记住手机号的方法就是将字母与数字对应,如图.这样就可以只记住一些单词,而不用记住数字.给出一个数字串和n个单词,用最少的单词数来代替数字串,输出对应的 ...

  8. Ural 1248 Sequence Sum 题解

    目录 Ural 1248 Sequence Sum 题解 题意 题解 程序 Ural 1248 Sequence Sum 题解 题意 给定\(n\)个用科学计数法表示的实数\((10^{-100}\s ...

  9. Ural 1201 Which Day Is It? 题解

    目录 Ural 1201 Which Day Is It? 题解 题意 输入 输出 题解 程序 Ural 1201 Which Day Is It? 题解 题意 打印一个月历. 输入 输入日\((1\ ...

随机推荐

  1. centos7初始化脚本(转)

    #!/bin/bash # 描述: CentOS 初始化脚本 # 加载配置文件 if [ -n "${1}" ];then /bin/} fi # 可接受配置(shell 变量格式 ...

  2. Python互联网金融之用户增长的数据逻辑

    怎样看待和应用我们互联网金融中的数据? 怎样进行数据分析? 互联网金融数据分析的三个层面: (1)指标层面 建立指标体系,观察指标涨跌的情况 (2)行业框架 不同的行业对于各个指标的权重不同 复投率 ...

  3. java试题复盘——11月13日

    上: 10. AccessViolationException异常触发后,下列程序的输出结果为(   A   ) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 AccessViol ...

  4. Python学习日记(四)——Python基本数据类型梳理(int、str、list、tuple、dict)

    数字(int) 1.创建方式 n1 = 123 n2 = int(123) 2.内存分配 #共同用一个内存地址的情况 n1 = 123 n2 = n1 #用两个内存地址的情况 n1 = 123 n2 ...

  5. Java并发指南7:JUC的核心类AQS详解

    一行一行源码分析清楚AbstractQueuedSynchronizer 转自https://www.javadoop.com/post/AbstractQueuedSynchronizer#toc4 ...

  6. 第04组 Beta冲刺(1)

    队名:斗地组 组长博客:地址 作业博客:Beta冲刺(1/4) 各组员情况 林涛(组长) 过去两天完成了哪些任务: 1.分配展示任务 2.收集各个组员的进度 3.写博客 展示GitHub当日代码/文档 ...

  7. mongoose 安装及配置

    MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方>案.MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据 ...

  8. Spring事务知识点

    事务的传播属性 PROPAGATION_REQUIRED 如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中.// 最常用,@Transactional注解默认 PROPAGA ...

  9. Android 显示系统:Vsync机制

    一.Vsync简介: 屏幕的刷新过程是每一行从左到右(行刷新,水平刷新,Horizontal Scanning),从上到下(屏幕刷新,垂直刷新,Vertical Scanning).当整个屏幕刷新完毕 ...

  10. Android中利用jsoup解析html页面

    学习jsoup :jsoup学习网站 Android 中使用: 添加依赖 implementation 'org.jsoup:jsoup:1.10.1' 直接上代码: package com.load ...