hdu 4339 Query (set解法)

Query

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2063    Accepted Submission(s): 707


Problem Description
You are given two strings s1[0..l1], s2[0..l2] and Q - number of queries.
Your task is to answer next queries:
  1) 1 a i c - you should set i-th character in a-th string to c;
  2) 2 i - you should output the greatest j such that for all k (i<=k and k<i+j) s1[k] equals s2[k].
 

Input
The first line contains T - number of test cases (T<=25).
Next T blocks contain each test.
The first line of test contains s1.
The second line of test contains s2.
The third line of test contains Q.
Next Q lines of test contain each query:
  1) 1 a i c (a is 1 or 2, 0<=i, i<length of a-th string, 'a'<=c, c<='z')
  2) 2 i (0<=i, i<l1, i<l2)
All characters in strings are from 'a'..'z' (lowercase latin letters).
Q <= 100000.
l1, l2 <= 1000000.
 

Output
For each test output "Case t:" in a single line, where t is number of test (numbered from 1 to T).
Then for each query "2 i" output in single line one integer j.
 

Sample Input
  
  
1 aaabba aabbaa 7 2 0 2 1 2 2 2 3 1 1 2 b 2 0 2 3
 

Sample Output
  
  
Case 1: 2 1 0 1 4 1
 

Source

ps:此题解法有两种,线段树和set。

set解法:
将每个不相同的位置加入set中,利用set的成员函数lower_bound()就很好找到j了。更新的话,用erase()、insert()就ok了。

感想:
这题男神教的,感觉好经典。自己是会set的,但是就是不能把问题抽象出来,转化为某个数据结构或者模型,所以比赛时是做不出来的。抽象、建模好重要,独立思考也很重要,做一道题要举一反三,要将这道题用的知识提取出来,想一想这些知识能解决哪一类的问题,这样才能一题顶十题!

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <algorithm>
#define maxn 1000005
using namespace std;

int n,m,q,ans,l,l1,l2;
char s1[maxn],s2[maxn];
set<int>s;
set<int>::iterator its;

void solve()
{
    int i,j,a,t,pos;
    char c[5],prec;
    scanf("%d",&q);
    while(q--)
    {
        scanf("%d",&t);
        if(t==1)
        {
            scanf("%d%d%s",&a,&pos,c);
            if(pos>=l) continue ;// 这个位置很重要  如果pos>l的话就没意义了 还好自己debug出来了 O(∩_∩)O
            if(a==1)
            {
                prec=s1[pos];
                s1[pos]=c[0];
                if(prec!=s2[pos]&&c[0]==s2[pos]) s.erase(pos);
                else if(prec==s2[pos]&&c[0]!=s2[pos]) s.insert(pos);
            }
            else
            {
                prec=s2[pos];
                s2[pos]=c[0];
                if(prec!=s1[pos]&&c[0]==s1[pos]) s.erase(pos);
                else if(prec==s1[pos]&&c[0]!=s1[pos]) s.insert(pos);
            }
        }
        else
        {
            scanf("%d",&pos);
            its=s.lower_bound(pos);
            if(its!=s.end()) printf("%d\n",*its-pos);
            else printf("%d\n",l-pos);
        }
    }
}
int main()
{
    int i,j,k,t;
    scanf("%d",&t);
    for(k=1;k<=t;k++)
    {
        scanf("%s%s",s1,s2);
        l1=strlen(s1);
        l2=strlen(s2);
        l=l1<l2?l1:l2;
        s.clear();
        for(i=0;i<l;i++)
        {
            if(s1[i]!=s2[i]) s.insert(i);
        }
        printf("Case %d:\n",k);
        solve();
    }
    return 0;
}



 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于HDU4546问题,还可以使用优先队列(Priority Queue)来解决。以下是使用优先队列的解法思路: 1. 首先,将数组a进行排序,以便后续处理。 2. 创建一个优先队列(最小堆),用于存储组合之和的候选值。 3. 初始化优先队列,将初始情况(即前0个数的组合之和)加入队列。 4. 开始从1到n遍历数组a的元素,对于每个元素a[i],将当前队列中的所有候选值取出,分别加上a[i],然后再将加和的结果作为新的候选值加入队列。 5. 重复步骤4直到遍历完所有元素。 6. 当队列的大小超过k时,将队列中的最小值弹出。 7. 最后,队列中的所有候选值之和即为前k小的组合之和。 以下是使用优先队列解决HDU4546问题的代码示例: ```cpp #include <iostream> #include <vector> #include <queue> #include <functional> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); // 对数组a进行排序 priority_queue<long long, vector<long long>, greater<long long>> pq; // 最小堆 pq.push(0); // 初始情况,前0个数的组合之和为0 for (int i = 0; i < n; i++) { long long num = pq.top(); // 取出当前队列中的最小值 pq.pop(); for (int j = i + 1; j <= n; j++) { pq.push(num + a[i]); // 将所有加和结果作为新的候选值加入队列 num += a[i]; } if (pq.size() > k) { pq.pop(); // 当队列大小超过k时,弹出最小值 } } long long sum = 0; while (!pq.empty()) { sum += pq.top(); // 求队列中所有候选值之和 pq.pop(); } cout << sum << endl; return 0; } ``` 使用优先队列的方法可以有效地找到前k小的组合之和,时间复杂度为O(nklog(k))。希望这个解法对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值