1.两头牛一秒移动一单位长度,所以在两头牛均在移动时,它们所在单位奇偶性是一致的。这意味这两头牛一定不会在某个单位格中间相遇。
2.如果可以在中间相遇(这题不考虑),可以将运动的距离乘以二,这样就可以只枚举整点。
C++

#include<iostream>
using namespace std;
const int N = 1000000 + 10;
int a[N], b[N];
int main()
{
    int n, m;
    cin >> n >> m;
    int t = 0;
    while(n --)
    {
        int l;
        char c;
        cin >> l >> c;
        int v = 1;
        if(c == 'L')v = -1;
        while(l --)
        {
            t ++;
            a[t] = a[t - 1] + v;
        }
    }
    while(++ t < N)a[t] = a[t - 1];
    t = 0;
    while(m --)
    {
        int l;
        char c;
        cin >> l >> c;
        int v = 1;
        if(c == 'L')v = -1;
        while(l --)
        {
            t ++;
            b[t] = b[t - 1] + v;
        }
    }
    while(++ t < N)b[t] = b[t - 1];
    int ans = 0;
    for(int i = 1; i < N; i ++)
    {
        if(a[i] == b[i] && a[i - 1] != b[i - 1])ans ++;
    }
    cout << ans<<endl;
    return 0;
}

Java

public class Main{
    public static int N = 1000000 + 10;
    public static int[] a = new int[N];
    public static int[] b = new int[N];
    public static void main(String[] args)
    {
        Scanner cin = new Scanner(System.in);
        int n, m;
        n = cin.nextInt();
        m = cin.nextInt();
        int t = 0;
        while(n --> 0)
        {
            int l;
            char c;
            l = cin.nextInt();
            c = cin.next().charAt(0);
            int v = 1;
            if(c == 'L')v = -1;
            while(l --> 0)
            {
                t ++;
                a[t] = a[t - 1] + v;
            }
        }
        while(++ t < N)a[t] = a[t - 1];
        t = 0;
        while(m --> 0)
        {
            int l;
            char c;
            l = cin.nextInt();
            c = cin.next().charAt(0);
            int v = 1;
            if(c == 'L')v = -1;
            while(l --> 0)
            {
                t ++;
                b[t] = b[t - 1] + v;
            }
        }
        while(++ t < N)b[t] = b[t - 1];
        int res = 0 ;
        for(int i = 1; i < N; i ++)
        {
            if(a[i] == b[i] && a[i - 1] != b[i - 1])res ++;
        }
        System.out.print(res);
    }
}