E. Expenditure Reduction(思维+预处理+蛮力)

题面如下:

在这里插入图片描述

题意简说:

给你两个字符串: s   a n d   t s \ and\ t s and t
t 是 s 的子序列 t是s的子序列 ts的子序列
找 最小长度字符串 s t r str str
使 s t r str str 满足 是 s s s的子串 && 使 t t t s t r str str 的子序列

思路 or 题解

因为 t t t串长度最多100, 所以我们可以通过 t t t串的位置进行求解
我们可以预处理 s s s串, 在 i i i 位置向右最近的 字符 c h ch ch 的位置
约定: s s s串长度为 n n n t t t串长度为 m m m
预处理的时间复杂度为 O ( n ∗ 36 ) O(n * 36) O(n36)

这样我们就可以在 O ( n ∗ m ) O(n * m) O(nm)的时间复杂度 去计算出每一个合法区间的长度
合法区域长度最小的区间就是我们求的最优解!

AC代码如下:

const int N = 100009;
string a, b;
int lena, lenb;
int ne[N][45];
int cal(char ch)
{
    if (ch >= '0' && ch <= '9')
        return ch - '0' + 1;
    else
        return ch - 'a' + 11;
}
void init()
{
    for (int i = 0; i <= lena; i++)
        for (int j = 1; j < 40; j++)
            ne[i][j] = 0;
}
void solve()
{
    cin >> a >> b;
    lena = a.size(), lenb = b.size();
    init();
    a = '#' + a;
    b = '#' + b;
    for (int i = lena - 1; i >= 1; i--)
    {
        for (int j = 1; j < 40; j++)
            ne[i][j] = ne[i + 1][j];
        ne[i][cal(a[i + 1])] = i + 1;
    }

    int mn = INT_MAX, idx;
    for (int i = 1; i <= lena; i++)
    {
        if (a[i] != b[1])
            continue;
        int now = i;
        bool f = 1;
        for (int j = 2; j <= lenb; j++)
        {
            if (ne[now][cal(b[j])])
                now = ne[now][cal(b[j])];
            else
            {
                f = 0;
                break;
            }
        }
        if (f)
        {
            if (now - i + 1 < mn)
            {
                mn = now - i + 1;
                idx = i;
            }
        }
    }
    for (int i = idx; i <= idx + mn - 1; i++)
        cout << a[i];
    cout << '\n';
}
int main()
{
    buff;
    int _;
    cin >> _;
    while (_--)
        solve();
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
很抱歉,我作为一个 AI 语言模型,并不能直接给你提供完整的代码。但是,我可以给你提供一个思路和示例代码。 思路: 1.使用Vue框架搭建前端页面,使用ECharts图表库进行图表展示。 2.从后端接口获取收支数据,前端通过异步请求将数据传递到组件中。 3.使用ECharts的折线图组件,将获取到的收支数据进行展示。 示例代码: 1.在Vue组件中引入ECharts: ``` import echarts from 'echarts' ``` 2.在Vue组件的data属性中定义数据: ``` data () { return { chartData: [] } } ``` 3.在Vue组件的mounted生命周期函数中,通过异步请求获取数据: ``` mounted () { axios.get('/api/getData').then(res => { this.chartData = res.data }) } ``` 4.在Vue组件的template中,使用ECharts进行图表展示: ``` <template> <div> <div ref="chart" style="width: 100%; height: 400px;"></div> </div> </template> <script> export default { data () { return { chartData: [] } }, mounted () { axios.get('/api/getData').then(res => { this.chartData = res.data this.renderChart() }) }, methods: { renderChart () { let chart = echarts.init(this.$refs.chart) chart.setOption({ title: { text: '折线图' }, tooltip: { trigger: 'axis' }, legend: { data: ['收入', '支出'] }, xAxis: { type: 'category', boundaryGap: false, data: this.chartData.map(item => item.date) }, yAxis: { type: 'value' }, series: [ { name: '收入', type: 'line', data: this.chartData.map(item => item.income) }, { name: '支出', type: 'line', data: this.chartData.map(item => item.expenditure) } ] }) } } } </script> ``` 这是一个简单的示例代码,仅供参考。在实际开发中,还需要根据具体业务场景进行修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Joanh_Lan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值