POJ Evil Straw Warts Live (贪心)

智能灯光系统:基于PIC18F97J60与RGB灯带的沉浸式体验 智能灯光系统通过嵌入式微控制器(如PIC18F97J60)与高密度RGB灯带(如IN-PC55TBTRGB)的结合,实现了动态光效的可编程控制。其核心原理是利用PWM调光技术和网络通信协议(如TCP/IP),将灯光效果与音乐节奏、环境感应等外部输入实时同步。这种技术不仅提升了空间的美学体验,还广泛应用于智能家居、商业展示和娱乐场所。通过优化电源设计、信号传输和光学扩散方案,系统能够实现高达1670万色的平滑过渡和低延迟响应。特别是在音频可视化场景中,结合FFT算法和HSV色彩空间插值,可以创造出极具冲击力的 阅读详情

Evil Straw Warts Live
Time Limit: 2000MS
Memory Limit: 65536K
Total Submissions: 850
Accepted: 239

Description

A palindrome is a string of symbols that is equal to itself when reversed. Given an input string, not necessarily a palindrome, compute the number of swaps necessary to transform the string into a palindrome. By swap we mean reversing the order of two adjacent symbols. For example, the string "mamad" may be transformed into the palindrome "madam" with 3 swaps:
swap "ad" to yield "mamda"
swap "md" to yield "madma"
swap "ma" to yield "madam"

Input

The first line of input gives n, the number of test cases. For each test case, one line of input follows, containing a string of up to 8000 lowercase letters.

Output

Output consists of one line per test case. This line will contain the number of swaps, or "Impossible" if it is not possible to transform the input to a palindrome.

Sample Input

3
mamad
asflkj
aabb

Sample Output

3
Impossible
2


题目大意:给一个单词,为了让它变成一个回文串,允许相邻的两个数交换。求最少的交换次数,使得单词变成一个回文串。


解题思路:首先判断一下字母出现的奇数次的个数是否大于1, 大于1不满足。然后从左右开始遍历判断tmp[left] 和 tmp[right]是否相同,相同跳过, 不相同的话找到从左边开始第一个使得tmp[left + l] == tmp[right], 和从右边开始第一个使得tmp[left] == tmp[right + r]。比较l 和 r 的大小然后考虑该移动哪一边。

但是在这里我只用了temp[left]==temp[right-r],将所有的r求和即可。


 #include<iostream>
 #include<cstdio>
 #include<cstring>
 #include<algorithm>
 #include<cmath>
 using namespace std;
 char arr[10000];
 int b[28];
 int solve()
 {
     int ans=0,len=strlen(arr),high=len-1;
     for(int i=0;i<(len+1)/2-1;i++){
        if(b[arr[i]-'a']>1){
            for(int j=high;j>i;j--){
                if(arr[i]==arr[j]){
                    for(int k=j+1;k<=high;k++){
                        arr[k-1]=arr[k];
                        ans++;
                    }
                    arr[high]=arr[i];
                    high--;
                    break;
                }
            }
            b[arr[i]-'a']-=2;
        }
        else{
            swap(arr[i],arr[i+1]);
            ans++;
            --i;
        }
     }
     return ans;
 }
 int main()
 {
     int n;
     scanf("%d",&n);
     while(n--){
        memset(b,0,sizeof(b));
        scanf("%s",arr);
        for(int i=0;arr[i];++i){
            ++b[arr[i]-'a'];
        }
        int cnt=0;
        for(int i=0;i<27;i++){
            if(b[i]%2) cnt++;
        }
        if(cnt>=2){
            printf("Impossible\n");
            continue;
        }
        else{
            printf("%d\n",solve());
        }
     }
     return 0;
 }


深入解析.Net中SM2与sm-crypto的跨平台加解密实践 本文深入探讨了在.Net平台与前端sm-crypto库之间实现SM2国密算法跨平台加解密的实践方案。针对对接中常见的密钥格式、密文拼接顺序(C1C3C2与C1C2C3)等核心难点,提供了基于BouncyCastle的完整工具类实现、前后端联调示例及问题排查指南,旨在帮助开发者构建安全、兼容的数据加密通道。 阅读详情

相关推荐

深度学习---双层神经网络中矩阵的运算及其含义记录

深度学习---双层神经网络中矩阵的运算及其含义记录神经网络的正向传播与反向传播正向传播第一层的权重矩阵第一层的输入矩阵Z^1^ = W^1^X^1^ + b^1^A^1^ = σ(Z^1^)第二层的权重矩阵第二层的输入矩阵Z^2^ = W^2^A^1^ + b^2^反向传播dz^2^ = A^2^ - Ydw^2^ = 1/n dz^2^A^1T^db^2^ = 1/n np.sum(dz^2...

qq_43228441的博客 3746

“推恩令”——分治法

A-01 POJ1854A palindrome is a string of symbols that is equal to itself when reversed. Given an input string, not necessarily a palindrome, compute the number of swaps necessary to transform the stri

⁄(⁄ ⁄•⁄ω⁄•⁄ ⁄)⁄. 菜鸟不吃草! 456

FPGA AXI DDR3读写时序调试与FIFO缓存实战解析

本文深入解析FPGA中AXI协议与DDR3的读写时序调试,重点探讨FIFO缓存在数据流控制中的关键作用。通过实战案例详细讲解突发传输中的地址对齐、数据分片等常见问题,并提供状态机设计、仿真波形分析和性能优化技巧,帮助开发者高效解决高速数据存储场景下的时序同步与数据一致性问题。

blockchain9miner的博客 1118

算法篇:贪心算法判断回文移动次数

A palindrome is a string of symbols that is equal to itself when reversed. Given an input string, not necessarily a palindrome, compute the number of swaps necessary to transform the string into a palindrome. By swap we mean reversing the order of two adja

s_m_c的博客 398

详解最大似然估计(MLE)、最大后验概率估计(MAP),以及贝叶斯公式的理解

声明:本文为原创文章,发表于nebulaf91的csdn博客。欢迎转载,但请务必保留本信息,注明文章出处。 本文作者: nebulaf91 本文原始地址:最大似然估计(Maximum likelihood estimation, 简称MLE)和最大后验概率估计(Maximum a posteriori estimation, 简称MAP)是很常用的两种参数估计方法,如果不理解这两种方法的思路,很

nebulaf91的博客 33万+

嵌入式调光控制库:轻量级软启/渐变/记忆逻辑实现

调光控制是智能照明系统的核心基础功能,涉及亮度调节、人机交互与电气安全多重需求。其底层原理依赖于对相位角或PWM占空比的精确时序控制,并需融合软启动、渐变过渡、状态记忆等工程化逻辑。这类轻量级调光行为逻辑层,不直接操作硬件寄存器,而是以确定性、无动态内存、可配置的方式封装用户意图到目标亮度值(0–100%)的映射过程,显著提升产品鲁棒性与跨平台复用能力。广泛应用于TRIAC交流调光、LED直流PWM驱动及DALI/Matter等智能照明协议网关中,尤其适合资源受限的Cortex-M系列MCU和FreeRTO

weixin_28949937的博客 579

分治问题POJ1854_构造回文

题目A palindrome is a string of symbols that is equal to itself when reversed. Given an input string, not necessarily a palindrome, compute the number of swaps necessary to transform the string into a pa

厚积薄发,持之以恒 1093

消费级显卡跑大模型:RTX 3060 12G 部署 70 亿参数 AI 编程助手的优化实战

本文详细介绍了如何在消费级显卡RTX 3060 12G上,通过量化、投机解码和分页KV-Cache等核心优化技术,成功部署并流畅运行70亿参数的CodeLlama-7B编程助手。文章提供了从环境配置、模型准备到参数调优的完整实战笔记,实测显存占用降至7.3GB,生成速度达21.7 token/秒,为开发者和技术爱好者提供了可行的本地AI部署方案。

weixin_29053067的博客 1258

POJ 1854 - Evil Straw Warts Live

Description A palindrome is a string of symbols that is equal to itself when reversed. Given an input string, not necessarily a palindrome, compute the number of swaps necessary to transform the st

Dilthey's Blog 836

Zigbee通信(CC2530终端 + ESP32-C6协调器)

实现了CC2530 zigbee终端与ESP32-C6 zigbee协调器的通信交互。

Ecsply的博客 2564

独家改进|基于YOLO26的轻量级检测网络:参数量减半,精度不降反升

不要全层替换深度可分离卷积:P5深层对语义特征要求高,用标准卷积效果更好,只在P2、P3浅层用DWConv性价比最高注意力不是越多越好:每个阶段加一个就够,加在模块输出端,不要插在模块中间,否则部署容易出现算子不兼容问题轻量模型训练别贪快:初始学习率一定要调低,不然很容易前期震荡,后期收敛不上,精度卡在瓶颈小目标场景优先保留P2分支:如果对速度要求不是极致,保留P2检测头,小目标收益远大于速度损失。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 221

构造回文

一、题目描述        给定一个字符串s,你可以从中删除一些字符,使得剩下的串是一个回文串。如何删除才能使得回文串最长呢?输出需要删除的字符个数。 二、解题思路       1. 利用最长公共字符串匹配的思路来解题。       2. 得到字符串及自身反转字符串。              3. 构造二维数组,进行匹配。        三、注意事项       数组从1开始循...

菜鸟哆哆的专栏 750

基于STM32L4XX、HAL库的DP83822 以太网收发器(PHY)驱动程序设计

一、简介:DP83822是德州仪器(TI)推出的一款低功耗、高性能10/100Mbps以太网物理层收发器(PHY)。它具有以下主要特性:支持10BASE-T和100BASE-TX以太网标准低功耗设计,适合嵌入式应用MII/RMII接口,方便与微控制器连接内置自检和诊断功能支持自动协商和手动配置工业级温度范围(-40°C至+85°C)

zhusl6688的博客 526

uva 10716 Evil Straw Warts Live(贪心回文串)

这道题目我用了一上午才做出来,还是看的别人的思路,虽然没有看代码做的有点慢,代码能力还是得加强啊,思维 得缜密,不能想当然,要有依据,写上的代码要有准确度,省的以后还得慢慢调试 思路:贪心,每次都查看两端位置上的字母是否相等,若不相等就在里面查找能使他们相等且所需移动位置最少的那 个,然后交换,记录交换的距离,贪心的离最后一个由近及远找与第一个位置相等的,同理贪心从第一个位置找和最 后一个

勿忘初心 779

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

poj 1854 Evil Straw Warts Live

Evil Straw Warts Live Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 728   Accepted: 198 Description A palindrome is a string of symbols that is equal to

sunnykema的专栏 972

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

POJ 1854 Evil Straw Warts Live G++ dfs 分治 背

描述 回文是一串符号,它们在被反转时等于它自己。 给定输入字符串,不一定是回文,计算将字符串转换为回文所需的交换数。 交换意味着颠倒两个相邻符号的顺序。 例如,字符串“mamad”可以被转换为具有3个交换的回文“madam”: 交换“ad”产生“mamda” 交换“md”产生“madma” 交换“ma”产生“madam” 输入 第一行输入给出n,即测试用例的数量。 对

woniupengpeng的专栏 650

贪心算法之Evil Straw Warts Live

Evil Straw Warts Live Description A palindrome is astring of symbols that is equal to itself when reversed. Given an input string,not necessarily a palindrome, compute the number of swaps necessar

hqf1992的专栏 828

UVA 10716 Evil Straw Warts Live(贪心)

Problem D: Evil Straw Warts Live A palindrome is a string of symbols that is equal to itself when reversed. Given an input string, not necessarily a palindrome, compute the number of swaps necessary

Remilia's 1424

UVA 10716 Evil Straw Warts Live

Evil Straw Warts Live A palindrome is a string of symbols that is equal to itself when reversed. Given an input string, not necessarily a palindrome, compute the number of swaps necessary to transf

Occult 605

Evil Straw Warts Live uva+回文串+贪心

Evil Straw Warts Live A palindrome is a string of symbols that is equal to itself when reversed. Given an input string, not necessarily a palindrome, compute the number of swaps necessary to transfor

u012870383的专栏 791

UVaOJ10716 - Evil Straw Warts Live

Problem D: Evil Straw Warts Live A palindrome is a string of symbols that is equal to itself when reversed. Given an input string, not necessarily a palindrome, compute the number of swaps necessary

c随遇er安的专栏 1142

uva 10716 - Evil Straw Warts Live

Evil Straw Warts Live A palindrome is a string of symbols that is equal to itself when reversed. Given an input string, not necessarily a palindrome, compute the number of swaps necessary to transfor

risker_2013的专栏 1042

分治法POJ 1854

分治法:POJ1854 Evil Straw Warts Live 问题: Description A palindrome is a string of symbols that is equal to itself when reversed. Given an input string, not necessarily a palindrome, compute the number of ...

weixin_44778719的博客 755

回文距离计算

Njzy在对回文数的学习过程中发现了一个有趣的问题. 这个问题是求一个数的回文距离。一个数的回文距离的定义是它减去一个回文数的绝对值的最小值。 比如:121的回文距离就是0,因为|121-121|=0,123的回文距离是2,|123-121|=2 using System; using System.Collections.Generic; using System.Linq;

超纯的小白兔 1116

poj 1854 Evil Straw Warts Live 变成回文要几次

Evil Straw Warts Live Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1799 Accepted: 523 Description A palindrome is a string of symbols that is equal t...

weixin_38168173的博客 266
上一篇: POJ 3258 River Hopscotch(二分·最小距离最大)
下一篇: 数列上的分治(求逆序数)
hellohelloC
博客等级 码龄12年 15粉丝 107原创
评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值