找规律:String Change

本文介绍了一个字符串转换问题,探讨了如何通过交换字符并增加其字母顺序值来将一个字符串转换成另一个字符串的方法。提供了具体的实现代码及样例输入输出,帮助读者理解解决这类问题的思路。

String change

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 370    Accepted Submission(s): 166


Problem Description
In this problem you will receive two strings S 1 and S 2 that contain only lowercase letters.
Each time you can swap any two characters of S 1. After swap,both of the two letters will increase their value by one. If the previous letter is 'z',it will become 'a' after being swapped.
That is to say ,"a" becomes "b","b" becomes "c"....."z" becomes "a" and so on.
You can do the change operation in S 1 as many times as you want.
Please tell us whether you can change S 1 to S 2 after some operations or not.
 

Input
There are several cases.The first line of the input is a single integer T (T <= 41) which is the number of test cases.Then comes the T test cases .

For each case,the first line is S 1,the second line is S 2.S 1 has the same length as S 2 and the length of the string is between 2 and 60.
 

Output
For each case,output "Case #X: " first, X is the case number starting from 1.If it is possible change S 1 to S 2 output "YES",otherwise output "NO".
 

Sample Input
  
3 ab ba bac ddb aaabb cbccd
 

Sample Output
  
Case #1: NO Case #2: YES Case #3: YES
Hint
For the first case,it's impossible to change "ab" to "ba" . For the second case,swap(S1[0],S1[2])->swap(S1[1],S1[2]),meanwhile:bac->dac->ddb. For the third case,swap(S1[0],S1[3])->swap(S1[1],S1[2])->swap(S1[2],S1[3])->swap(S1[3],S1[4]), meanwhile:aaabb->caabb->cbbbb->cbccb->cbccd.
 

Author
miketc@UESTC_Goldfinger
 

Source


这个题完全是找规律= =。

当长度为2的时候特判一下,其他长度如果同奇偶就YES,否则NO

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>

using namespace std;

char str1[70], str2[70];
int num1[70], num2[70];

int main(){
    int t;
    int cnt = 0;
    scanf("%d", &t);
    while(t --){
        cnt ++;
        scanf("%s %s", str1, str2);
        int len = strlen(str1);
        for(int i = 0; i < len; i ++){
            num1[i] = str1[i] - 'a' + 1;
            num2[i] = str2[i] - 'a' + 1;
        }
        printf("Case #%d: ", cnt);
        if(len == 2){
            if(num1[0] - num2[0] == num1[1] - num2[1] && (num1[0] - num2[0]) % 2 == 0)
                printf("YES\n");
            else if(num1[0] - num2[1] == num1[1] - num2[0] && (int)abs((float)num1[0] - (float)num2[1]) % 2)
                printf("YES\n");
            else
                printf("NO\n");
        }
        else{
            int sum = 0;
            for(int i = 0; i < len; i ++){
                sum += num1[i] - num2[i];
            }
            if((int)abs((sum)) % 2 == 0)
                printf("YES\n");
            else
                printf("NO\n");
        }
    }
    return 0;
}


我是有一个堆外缓存的测试,我想测试我插入数据的大小,所以需要观察堆外内存的是应用情况,你发的代码我无法直接用 @SpringBootTest class CacheServiceOhcImplTest { private static final int ENTRT_COUNT = 1000000; // @Mock // private CacheManager cacheManager; // @Mock // private CacheProperties cacheProperties; // @InjectMocks // private CacheServiceOhcImpl cacheServiceOhcImpl; @Autowired private CacheService cacheService; @Autowired private CacheManager cacheManager; private final String cacheName = "name1"; @Test void get() { String key = "test"; String result = cacheService.get(cacheName, key, String.class); System.out.println(result); } @Test void set() { String key = "test"; cacheService.set(cacheName,key,"11"); cacheService.set(cacheName,"testlong",123465789L); String result = cacheService.get(cacheName, key, String.class); Long testlong = cacheService.get(cacheName, "testlong", Long.class); System.out.println(result); System.out.println(testlong); Map<String, String> stringMap = new HashMap<>(); stringMap.put("key", "test"); stringMap.put("value", "11"); stringMap.put("key2", "testlong"); stringMap.put("value2", "123465789"); cacheService.set(cacheName,"stringMap",stringMap); Map stringMap1 = cacheService.get(cacheName, "stringMap", Map.class); System.out.println(stringMap1); } @Test public void testWriteReadSpeed(){ long startTime = System.nanoTime(); for (int i = 0; i < ENTRT_COUNT; i++) { cacheService.set(cacheName, "test" + i, "test" + i); } long writeTime = System.nanoTime() - startTime; System.out.println(cacheName + " write time : " + TimeUnit.NANOSECONDS.toMillis(writeTime) + " ms"); long startreadTime = System.nanoTime(); for (int i = 0; i < ENTRT_COUNT; i++) { cacheService.get(cacheName,"test"+i,String.class); } long readTime = System.nanoTime() - startreadTime; System.out.println(cacheName + " read time : " + TimeUnit.NANOSECONDS.toMillis(readTime) + " ms"); OHCache<String, Wrapper> cache = cacheManager.getCache(cacheName); long size = cache.size(); System.out.println(size); long capacity = cache.capacity(); System.out.println(capacity);}
最新发布
09-29
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值