UVAL 7902 2016ECfinal F - Mr. Panda and Fantastic Beasts

本文介绍了一种求解最短且字典序最小的唯一子串算法,通过将多个字符串连接并使用特殊字符隔开,然后利用后缀数组和LCP数组来高效查找满足条件的子串。算法详细讲解了如何构建后缀数组、计算高度数组以及如何确定答案的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:

给出n个串,求一个最短的第一个串的子串使它不在其他的n-1个串中出现,若有多个求字典序最小的。

Limits: • 1 ≤ T ≤ 42. • 2 ≤ N ≤ 50000. • N ≤ S1 + S2 + · · · + SN ≤ 250000. • the sum of Si in all test cases doesn’t exceed 3 × 106 .

Sample Input

3

2

aba

bab

3

qnu

cvbb

bnu

3

a

aa

aaa

Sample Output

Case #1: aba

Case #2: q

Case #3: Impossible

 

代码:

//这么多串肯定要把他们连接起来(之间用没出现的字符隔开)。后缀数组求出heigh数组,答案要求字典序最小所以后缀数组从前向后找即可,每找到
//一个在第一个串中的i位置时,在该位置向前和向后各找到第一个不在第一个串中的j那么lcp(i,j)一定是i位置和其他不在第一个串中后缀的
//最长的lcp,那么这个lcp+1的长度就一定是在其他串中没有出现过,要求字典序最小所以要向后扩展一位,并且这个长度不能超出第一个串。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=300000;
int t,n,sa[MAXN],he[MAXN],ra[MAXN],xx[MAXN],yy[MAXN],buc[MAXN];
char s[MAXN],ch[MAXN];
int len,len1,m;
void get_suf()
{
    int *x=xx,*y=yy;
    for(int i=0;i<m;i++) buc[i]=0;
    for(int i=0;i<len;i++) buc[x[i]=s[i]]++;
    for(int i=1;i<m;i++) buc[i]+=buc[i-1];
    for(int i=len-1;i>=0;i--) sa[--buc[x[i]]]=i;
    for(int k=1;k<=len;k<<=1){
        int p=0;
        for(int i=len-1;i>=len-k;i--) y[p++]=i;
        for(int i=0;i<len;i++) if(sa[i]>=k) y[p++]=sa[i]-k;
        for(int i=0;i<m;i++) buc[i]=0;
        for(int i=0;i<len;i++) buc[x[y[i]]]++;
        for(int i=1;i<m;i++) buc[i]+=buc[i-1];
        for(int i=len-1;i>=0;i--) sa[--buc[x[y[i]]]]=y[i];
        swap(x,y);
        p=1;x[sa[0]]=0;
        for(int i=1;i<len;i++){
            if(y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k])
                x[sa[i]]=p-1;
            else x[sa[i]]=p++;
        }
        if(p>=len) break;
        m=p;
    }
    for(int i=0;i<len;i++) ra[sa[i]]=i;
    int k=0;
    for(int i=0;i<len;i++){
        if(ra[i]==0) { he[0]=0;continue; }
        if(k) k--;
        int j=sa[ra[i]-1];
        while(s[i+k]==s[j+k]&&i+k<len&&j+k<len) k++;
        he[ra[i]]=k;
    }
}
void solve()
{
    int ans=len,pos=-1;
    for(int i=0;i<len;i++){
        while(i<len&&sa[i]>=len1) i++;
        if(i>=len) break;
        int plcp=he[i];
        for(int j=i-1;j>=0;j--){
            if(sa[j]>=len1) break;
            plcp=min(plcp,he[j]);
        }
        int slcp=he[i+1];
        for(int j=i+1;j<=len;j++){
            if(sa[j]>=len1) break;
            slcp=min(slcp,he[j]);
        }
        plcp=max(plcp,slcp);
        if(plcp<len1-sa[i]){
            if(plcp+1<ans){
                ans=plcp+1;
                pos=sa[i];
            }
        }
    }

    if(pos==-1) puts("Impossible");
    else{
        for(int i=0;i<ans;i++)
            printf("%c",s[i+pos]);
        puts("");
    }
}
int main()
{
    scanf("%d",&t);
    for(int cas=1;cas<=t;cas++){
        scanf("%d",&n);
        scanf("%s",s);
        len=len1=strlen(s);
        for(int i=2;i<=n;i++){
            s[len++]='#';
            scanf("%s",s+len);
            len=strlen(s);
        }
        m=200;
        get_suf();
        printf("Case #%d: ",cas);
        solve();
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/--ZHIYUAN/p/7976646.html

package com.example.ball.ui.activity; import android.graphics.Color; import android.os.Build; import android.os.Bundle; import android.util.Log; import android.util.Size; import android.view.View; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.RequiresApi; import androidx.appcompat.app.AppCompatActivity; import androidx.camera.core.CameraSelector; import androidx.camera.core.ImageAnalysis; import androidx.camera.core.ImageProxy; import androidx.camera.core.Preview; import androidx.camera.lifecycle.ProcessCameraProvider; import androidx.camera.view.PreviewView; import androidx.core.content.ContextCompat; import com.example.ball.R; import com.google.common.util.concurrent.ListenableFuture; import java.nio.ByteBuffer; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class CameraActivity extends AppCompatActivity { private static final String TAG = "ColorRecognition"; private static final int SAMPLE_SIZE = 5; // 采样区域半径 private static final int COLOR_TOLERANCE = 80; // 颜色匹配容差 // 预设的10种颜色及其名称 private static final Map<Integer, String> PRESET_COLORS = new HashMap<Integer, String>() {{ put(Color.RED, "红色"); put(Color.GREEN, "绿色"); put(Color.BLUE, "蓝色"); put(Color.YELLOW, "黄色"); put(Color.CYAN, "青色"); put(Color.MAGENTA, "品红"); put(0xFFFFA500, "橙色"); // 橙色 put(0xFF800080, "紫色"); // 紫色 put(0xFFFFC0CB, "粉色"); // 粉色 put(0xFFA52A2A, "棕色"); // 棕色 }}; private PreviewView previewView; private View targetArea; private View colorPreview; private TextView tvColorName; private TextView tvColorInfo; private TextView tvDetectionStatus; private ExecutorService cameraExecutor; private int targetX = 0; // 目标区域中心X坐标 private int targetY = 0; // 目标区域中心Y坐标 @RequiresApi(api = Build.VERSION_CODES.M) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_camera); previewView = findViewById(R.id.previewView); targetArea = findViewById(R.id.targetArea); colorPreview = findViewById(R.id.colorPreview); tvColorName = findViewById(R.id.tvColorName); tvColorInfo = findViewById(R.id.tvColorInfo); tvDetectionStatus = findViewById(R.id.tvDetectionStatus); cameraExecutor = Executors.newSingleThreadExecutor(); // 设置目标区域位置(屏幕中心) previewView.post(() -> { targetX = previewView.getWidth() / 2; targetY = previewView.getHeight() / 2; updateTargetPosition(); }); // 启动摄像头 if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) == android.content.pm.PackageManager.PERMISSION_GRANTED) { startCamera(); } else { requestPermissions(new String[]{android.Manifest.permission.CAMERA}, 1); } } private void updateTargetPosition() { targetArea.setX(targetX - targetArea.getWidth() / 2f); targetArea.setY(targetY - targetArea.getHeight() / 2f); } private void startCamera() { ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this); cameraProviderFuture.addListener(() -> { try { ProcessCameraProvider cameraProvider = cameraProviderFuture.get(); // 配置预览 Preview preview = new Preview.Builder().build(); preview.setSurfaceProvider(previewView.getSurfaceProvider()); // 配置图像分析 ImageAnalysis imageAnalysis = new ImageAnalysis.Builder() .setTargetResolution(new Size(640, 480)) // 优化性能 .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST) .build(); imageAnalysis.setAnalyzer(cameraExecutor, new ColorAnalyzer()); // 选择后置摄像头 CameraSelector cameraSelector = new CameraSelector.Builder() .requireLensFacing(CameraSelector.LENS_FACING_BACK) .build(); // 绑定用例 cameraProvider.bindToLifecycle(this, cameraSelector, preview, imageAnalysis); } catch (ExecutionException | InterruptedException e) { Log.e(TAG, "Camera setup error: " + e.getMessage()); } }, ContextCompat.getMainExecutor(this)); } private class ColorAnalyzer implements ImageAnalysis.Analyzer { @Override public void analyze(@NonNull ImageProxy image) { // 坐标转换(预览坐标 → 图像坐标) int imgWidth = image.getWidth(); int imgHeight = image.getHeight(); int previewWidth = previewView.getWidth(); int previewHeight = previewView.getHeight(); int imgX = (int) (targetX * (imgWidth / (float) previewWidth)); int imgY = (int) (targetY * (imgHeight / (float) previewHeight)); // 从YUV数据提取颜色 int color = extractColor(image, imgX, imgY); // 更新UI runOnUiThread(() -> updateColorDisplay(color)); image.close(); } private int extractColor(ImageProxy image, int centerX, int centerY) { ImageProxy.PlaneProxy[] planes = image.getPlanes(); int rSum = 0, gSum = 0, bSum = 0; int count = 0; // 采样区域:中心点周围5x5像素 for (int dy = -SAMPLE_SIZE; dy <= SAMPLE_SIZE; dy++) { for (int dx = -SAMPLE_SIZE; dx <= SAMPLE_SIZE; dx++) { int x = centerX + dx; int y = centerY + dy; if (x >= 0 && x < image.getWidth() && y >= 0 && y < image.getHeight()) { int color = getRGBFromYUV(planes, x, y, image.getWidth()); rSum += Color.red(color); gSum += Color.green(color); bSum += Color.blue(color); count++; } } } // 计算平均颜色 if (count > 0) { return Color.rgb(rSum / count, gSum / count, bSum / count); } return Color.BLACK; } // 高效YUV转RGB private int getRGBFromYUV(ImageProxy.PlaneProxy[] planes, int x, int y, int width) { ByteBuffer yBuffer = planes[0].getBuffer(); ByteBuffer uBuffer = planes[1].getBuffer(); ByteBuffer vBuffer = planes[2].getBuffer(); // 计算偏移量 int yOffset = y * planes[0].getRowStride() + x * planes[0].getPixelStride(); int uvX = x / 2; int uvY = y / 2; int uOffset = uvY * planes[1].getRowStride() + uvX * planes[1].getPixelStride(); int vOffset = uvY * planes[2].getRowStride() + uvX * planes[2].getPixelStride(); // 获取YUV值 int yVal = yBuffer.get(yOffset) & 0xFF; int uVal = uBuffer.get(uOffset) & 0xFF; int vVal = vBuffer.get(vOffset) & 0xFF; // YUV转RGB公式 int r = clamp((int) (yVal + 1.402 * (vVal - 128))); int g = clamp((int) (yVal - 0.34414 * (uVal - 128) - 0.71414 * (vVal - 128))); int b = clamp((int) (yVal + 1.772 * (uVal - 128))); return Color.rgb(r, g, b); } private int clamp(int value) { return Math.max(0, Math.min(255, value)); } } private void updateColorDisplay(int detectedColor) { // 更新颜色预览 colorPreview.setBackgroundColor(detectedColor); // 获取RGB值 int r = Color.red(detectedColor); int g = Color.green(detectedColor); int b = Color.blue(detectedColor); String rgb = String.format("RGB: %d, %d, %d", r, g, b); tvColorInfo.setText(rgb); // 匹配预设颜色 String colorName = "未设定颜色"; for (Map.Entry<Integer, String> entry : PRESET_COLORS.entrySet()) { int presetColor = entry.getKey(); int pr = Color.red(presetColor); int pg = Color.green(presetColor); int pb = Color.blue(presetColor); // 计算颜色距离 double distance = Math.sqrt( Math.pow(r - pr, 2) + Math.pow(g - pg, 2) + Math.pow(b - pb, 2) ); if (distance < COLOR_TOLERANCE) { colorName = entry.getValue(); break; } } tvColorName.setText(colorName); tvDetectionStatus.setText(colorName.equals("未设定颜色") ? "识别区域" : "检测到: " + colorName); } @Override protected void onDestroy() { super.onDestroy(); if (cameraExecutor != null) { cameraExecutor.shutdown(); } } } 以上预设的颜色由于识别不够精准导致与需求不符,请改成预设颜色范围
08-03
内容概要:本文深入探讨了Kotlin语言在函数式编程和跨平台开发方面的特性和优势,结合详细的代码案例,展示了Kotlin的核心技巧和应用场景。文章首先介绍了高阶函数和Lambda表达式的使用,解释了它们如何简化集合操作和回调函数处理。接着,详细讲解了Kotlin Multiplatform(KMP)的实现方式,包括共享模块的创建和平台特定模块的配置,展示了如何通过共享业务逻辑代码提高开发效率。最后,文章总结了Kotlin在Android开发、跨平台移动开发、后端开发和Web开发中的应用场景,并展望了其未来发展趋势,指出Kotlin将继续在函数式编程和跨平台开发领域不断完善和发展。; 适合人群:对函数式编程和跨平台开发感兴趣的开发者,尤其是有一定编程基础的Kotlin初学者和中级开发者。; 使用场景及目标:①理解Kotlin中高阶函数和Lambda表达式的使用方法及其在实际开发中的应用场景;②掌握Kotlin Multiplatform的实现方式,能够在多个平台上共享业务逻辑代码,提高开发效率;③了解Kotlin在不同开发领域的应用场景,为选择合适的技术栈提供参考。; 其他说明:本文不仅提供了理论知识,还结合了大量代码案例,帮助读者更好地理解和实践Kotlin的函数式编程特性和跨平台开发能力。建议读者在学习过程中动手实践代码案例,以加深理解和掌握。
内容概要:本文深入探讨了利用历史速度命令(HVC)增强仿射编队机动控制性能的方法。论文提出了HVC在仿射编队控制中的潜在价值,通过全面评估HVC对系统的影响,提出了易于测试的稳定性条件,并给出了延迟参数与跟踪误差关系的显式不等式。研究为两轮差动机器人(TWDRs)群提供了系统的协调编队机动控制方案,并通过9台TWDRs的仿真和实验验证了稳定性和综合性能改进。此外,文中还提供了详细的Python代码实现,涵盖仿射编队控制类、HVC增强、稳定性条件检查以及仿真实验。代码不仅实现了论文的核心思想,还扩展了邻居历史信息利用、动态拓扑优化和自适应控制等性能提升策略,更全面地反映了群体智能协作和性能优化思想。 适用人群:具备一定编程基础,对群体智能、机器人编队控制、时滞系统稳定性分析感兴趣的科研人员和工程师。 使用场景及目标:①理解HVC在仿射编队控制中的应用及其对系统性能的提升;②掌握仿射编队控制的具体实现方法,包括控制器设计、稳定性分析和仿真实验;③学习如何通过引入历史信息(如HVC)来优化群体智能系统的性能;④探索中性型时滞系统的稳定性条件及其在实际系统中的应用。 其他说明:此资源不仅提供了理论分析,还包括完整的Python代码实现,帮助读者从理论到实践全面掌握仿射编队控制技术。代码结构清晰,涵盖了从初始化配置、控制律设计到性能评估的各个环节,并提供了丰富的可视化工具,便于理解和分析系统性能。通过阅读和实践,读者可以深入了解HVC增强仿射编队控制的工作原理及其实际应用效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值