CF 1005B Delete from the Left 【模拟数组操作/正难则反】

You are given two strings s and t. In a single move, you can choose any of two strings and delete the first (that is, the leftmost) character. After a move, the length of the string decreases by 1. You can‘t choose a string if it is empty.

For example:

by applying a move to the string "where", the result is the string "here",
by applying a move to the string "a", the result is an empty string "".
You are required to make two given strings equal using the fewest number of moves. It is possible that, in the end, both strings will be equal to the empty string, and so, are equal to each other. In this case, the answer is obviously the sum of the lengths of the initial strings.

Write a program that finds the minimum number of moves to make two given strings s and t equal.

Input
The first line of the input contains s. In the second line of the input contains t. Both strings consist only of lowercase Latin letters. The number of letters in each string is between 1 and 2?105, inclusive.

Output
Output the fewest number of moves required. It is possible that, in the end, both strings will be equal to the empty string, and so, are equal to each other. In this case, the answer is obviously the sum of the lengths of the given strings.

Examples
Input
test
west
Output
2
Input
codeforces
yes
Output
9
Input
test
yes
Output
7
Input
b
ab
Output
1
Note
In the first example, you should apply the move once to the first string and apply the move once to the second string. As a result, both strings will be equal to "est".

In the second example, the move should be applied to the string "codeforces" 8 times. As a result, the string becomes "codeforces" → "es". The move should be applied to the string "yes" once. The result is the same string "yes" → "es".

In the third example, you can make the strings equal only by completely deleting them. That is, in the end, both strings will be empty.

In the fourth example, the first character of the second string should be deleted.

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const ll LNF = 1e18;
const int N = 1e3 + 20;
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
string a,b;
/*
给你两个字符串,只能选其中一个的最左字符删除,若不能删到相等就输出两串长之和,否则输出删除字符数
*/
int main()
{
    while(cin>>a>>b)
    {
        int c=0;
        reverse(a.begin(),a.end());
        reverse(b.begin(),b.end());
        rep(i,0,min(a.size(),b.size()))
        {
            if(a[i]==b[i]) c++;
            else break;
        }
        cout<<a.size()+b.size()-2*c<<endl;
    }
}

原文地址:https://www.cnblogs.com/Roni-i/p/9347994.html

时间: 2024-07-30 22:30:53

CF 1005B Delete from the Left 【模拟数组操作/正难则反】的相关文章

51nod 1050 循环数组最大子段和【环形DP/最大子段和/正难则反】

1050 循环数组最大子段和 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 N个整数组成的循环序列a[1],a[2],a[3],…,a[n],求该序列如a[i]+a[i+1]+…+a[j]的连续的子段和的最大值(循环序列是指n个数围成一个圈,因此需要考虑a[n-1],a[n],a[1],a[2]这样的序列).当所给的整数均为负数时和为0. 例如:-2,11,-4,13,-5,-2,和最大的子段为:11,-4,13.和为20. Input 第1

数据结构(1):使用面向对象模拟数组

数组是一种常用的数据结构,数组具有不可变性,创建后的数组的长度固定,通过索引访问数组中的元素,访问速度快,删除添加效率低. 通过面向对象模拟数组,模拟的数组具有以下功能: 添加新元素 展示 查找元素所在位置 根据索引获取元素 根据索引删除元素 修改指定位置的元素 同时使用两个算法对数组进行操作: 有序添加元素 二分查找法 1.创建数组类 MyArray.java 数据如何存储呢?在类中添加一个数组类型的私有属性用来保存数据,同时添加一个变量存储有效数据的长度(也就是元素的个数) 创建数组的时候需

sql server 模拟数组【转】

sql server 模拟数组 --SQL对字符串的处理能力比较弱,比如我要循环遍历象1,2,3,4,5这样的字符串,如果用数组的话,遍历很简单,但是T-SQL不支持数组,所以处理下来比较麻烦.下边的函数,实现了象数组一样去处理字符串.一,用临时表作为数组create function f_split(@c varchar(2000),@split varchar(2)) returns @t table(col varchar(20)) as begin while(charindex(@sp

js数组操作

js数组操作大全(转) shift:删除原数组第一项,并返回删除元素的值:如果数组为空则返回undefined var a = [1,2,3,4,5]; var b = a.shift(); //a:[2,3,4,5] b:1 unshift:将参数添加到原数组开头,并返回数组的长度 var a = [1,2,3,4,5]; var b = a.unshift(-2,-1); //a:[-2,-1,1,2,3,4,5] b:7 注:在IE6.0下测试返回值总为undefined,FF2.0下测试

JavaScript的数组操作

JavaScript的数组操作 JavaScript数组也是对象,它使用单一的变量存储一系列的值. 数组和对象的区别 在JavaScript中,数组必须使用数字索引,对象可以使用命名索引. 数组是特殊类型的对象,具有特有的一些属性和方法. 如何区分数组和对象 方案1 ECMAScript5定义新方法Array.isArray() var arr = []; var obj = {}; console.log(Array.isArray(arr)); // true console.log(Arr

JavaScript中常见的数组操作函数及用法

昨天写了个帖子,汇总了下常见的JavaScript中的字符串操作函数及用法.今天正好有时间,也去把JavaScript中常见的数组操作函数及用法总结一下,这样方便大家准备参考.如果恰好你也在准备各种笔试,希望对你有所帮助.同时,也欢迎补充. 1.数组创建 创建数组应该是最简单的了,有用数组字面量创建和数组构造函数两种方法,见下: var array1 = new Array(); var array2 = []; 上面是创建数组的最常见的两种方法,其中第二种方法因为简单直观而被开发者推崇.其中,

【数据结构】数组操作(LowArrayApp.java)

1 // LowArrayApp.java 2 // demonstrates array class with low-level interface 3 // to run this program: C>java LowArrayAPP 4 //java数据结构和算法(第二版)拉佛 著 pdf第46页 数组操作 5 6 package first; 7 8 class LowArray 9 { 10 private long[] a; // ref to array a 11 12 pub

PHP数组操作大全

1 <?php 2 /** 3 * File: phpstudy : array_test.php 4 * Created by PhpStorm. 5 * User: IhMfLy [email protected] 6 * Date: 2016-08-03 7 * Time: 10:36 8 */ 9 24 25 26 # 创建数组 27 $arr1_0 = array(); 28 $arr1_0[] = 23; #创建,再初始化 29 $arr1_0[] = "lisi";

[记录] JavaScript 中的数组操作

1. 定义方式:    var arr = []; // 数组字面量    var arr = new Array(); // 数组构造函数 数组所有方法都来源于 Array.prototype 2. 数组的读和写:    arr[num]  // 不可以溢出读  结果undefined    arr[num] = xxx; // 可以溢出写 3. 改变原数组    push()    // 在数组最后一位添加数据,返回值是数组的长度:    模拟数组方法:    Array.prototype